Skip to content

Commit

Permalink
feat: build cli command
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelramos committed Jan 4, 2022
1 parent b32cbef commit c0a804b
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 9 deletions.
6 changes: 6 additions & 0 deletions .changeset/lemon-eagles-remember.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@websublime/vtx-cli': patch
'@websublime/vtx-common': patch
---

Build cli command for apps and libs
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ vtx create-workspace
```

And answer to the questions from the cli to config your workspace.
Vite config produced will have a config to resolve every entry from your workspace and also will automatic resolve alias for your apps and libs that are created with this tool.

### Vue App
After creating your workspace you are ready to add as many apps as you want. Inside your workspace directory run:
Expand Down Expand Up @@ -106,6 +107,24 @@ Or you can adjust your package.json script to run different apps like:

Vtx supports all options from vite-cli plus --app and --lib.

### Build

To build just run:

```
vtx build --app todo
```

For type application will produce dist normally. The particular case goes for lib as:

```
vtx build --lib services
```

When your lib as created, is package.json as config entry for rollupOptions where you can pass vite rollupOtions as vite documentation describe. The lib will be created with the name that to you gave it and get necessary options from workspace package.json.

It is mandatory to indicate which type and name you are building.

### Config

Vtx saves the configuration on root of your workspace in package.json. You can review it there.
Expand Down Expand Up @@ -162,4 +181,4 @@ More info about the tool
## Publish

- Run yarn changeset on branch
- Request PR
- Request PR
36 changes: 32 additions & 4 deletions packages/cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { promptCreateWorkspace, promptCreateApp, promptCreateLib } from './promp
import { copy, writeJson, readJson } from 'fs-extra';
import { join, resolve } from 'path';
import { toValidPackageName } from '@websublime/vtx-common';
import { viteServer } from './vite';
import { viteBuild, viteServer } from './vite';
import { EOL } from 'os';

const cli = cac('vtx');
Expand Down Expand Up @@ -199,14 +199,42 @@ cli.command('build [root]')
.option('-w, --watch', `[boolean] rebuilds when modules have changed on disk`)
.action(async (root: string, options: any) => {
const target = root || process.cwd();
const { lib, app } = options;
const { lib = null, app = null, ...rest } = options;

if(!lib && !app) {
console.error('Please define which app or lib to build!');
process.exit();
process.exit(1);
}

console.info(`BUILD PROCESS on ${target}`);
const name: string = app || lib;
const pkgWorkspace = await readJson(resolve(join(target, './package.json')));

const entry = Object.entries(pkgWorkspace.config.packages).find(([key]) => key === name);

if(!entry || !entry.length) {
console.error(`The ${name} is not defined on vtx config.`);
process.exit(1);
}

const [_, value] = entry as [string, Record<string, string>];
const isLib = value.type === 'lib';
const pkg = await readJson(resolve(join(value.dir, './package.json')));

let buildOptions = JSON.parse(JSON.stringify(rest));

if(isLib) {
buildOptions = {
...buildOptions,
lib: {
entry: resolve(join(value.dir, pkg.source)),
fileName: (format: string) => `${value.name}.${format}.js`,
name: value.name
},
rollupOptions: pkg.config.rollupOptions
};
}

await viteBuild(value.dir, options, buildOptions);
});

cli.help();
Expand Down
3 changes: 3 additions & 0 deletions packages/cli/src/templates/lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
"scripts": {
"build": "tsc"
},
"config": {
"rollupOptions": {}
},
"dependencies": {},
"devDependencies": {
"@types/node": "^16.11.12",
Expand Down
23 changes: 19 additions & 4 deletions packages/cli/src/vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,23 @@ export const viteServer = async (root: string, options: any, httpOptions: any) =
}
};

export const viteBuild = async (root: string, options: BuildOptions) => {
console.info(root);
export const viteBuild = async (root: string, options:any, buildOptions: BuildOptions) => {
try {
await build({
root,
base: options.base,
mode: options.mode,
configFile: options.config || './vite.config.js',
logLevel: options.logLevel,
clearScreen: options.clearScreen,
build: buildOptions
})
} catch (e: any) {
createLogger(options.logLevel).error(
chalk.red(`error during build:\n${e.stack}`),
{ error: e }
);

await build(options);
};
process.exit(1)
}
};

0 comments on commit c0a804b

Please sign in to comment.