flecks/packages/create-app/build/cli.js

69 lines
1.9 KiB
JavaScript
Raw Normal View History

2024-01-16 00:28:20 -06:00
#!/usr/bin/env node
2024-01-20 04:59:13 -06:00
const {join} = require('path');
2022-02-25 04:58:08 -06:00
2024-01-20 04:59:13 -06:00
const {
2024-01-04 03:20:55 -06:00
Option,
program,
2024-01-20 04:59:13 -06:00
} = require('@flecks/core/build/commands');
const {
dumpYml,
loadYml,
2024-01-04 03:20:55 -06:00
transform,
2024-01-20 04:59:13 -06:00
} = require('@flecks/core/server');
const validate = require('validate-npm-package-name');
2022-02-25 04:58:08 -06:00
2024-01-20 04:59:13 -06:00
const build = require('./build');
const {move, testDestination} = require('./move');
2022-02-25 04:58:08 -06:00
2022-03-01 01:52:56 -06:00
const {
FLECKS_CORE_ROOT = process.cwd(),
} = process.env;
2022-02-25 04:58:08 -06:00
2022-03-01 01:52:56 -06:00
(async () => {
2024-01-04 03:20:55 -06:00
program.argument('<app>', 'name of the app to create');
program.addOption(
2024-01-16 00:28:20 -06:00
new Option('-pm,--package-manager <binary>', 'package manager binary')
2024-01-04 03:20:55 -06:00
.choices(['npm', 'bun', 'yarn'])
.default('npm'),
);
program.action(async (app, {packageManager}) => {
try {
const {errors} = validate(app);
if (errors) {
throw new Error(`@flecks/create-app: invalid app name: ${errors.join(', ')}`);
}
const destination = join(FLECKS_CORE_ROOT, app);
2024-01-04 18:39:21 -06:00
const name = app.startsWith('@') ? app : `@${app}/monorepo`;
2024-01-04 03:20:55 -06:00
if (!await testDestination(destination)) {
const error = new Error(
`@flecks/create-app: destination '${destination} already exists: aborting`,
);
error.code = 129;
throw error;
}
2024-01-20 04:59:13 -06:00
const fileTree = await move(name, join(__dirname, '..', 'template'));
2024-01-04 03:20:55 -06:00
fileTree.pipe(
'build/flecks.yml',
transform((chunk, encoding, done, stream) => {
const yml = loadYml(chunk);
2024-01-20 04:59:13 -06:00
yml['@flecks/core'] = {id: app};
2024-01-04 18:39:21 -06:00
if ('npm' !== packageManager) {
2024-01-20 04:59:13 -06:00
yml['@flecks/core'].packageManager = packageManager;
2024-01-04 18:39:21 -06:00
}
stream.push(dumpYml(yml, {forceQuotes: true, sortKeys: true}));
2024-01-04 03:20:55 -06:00
done();
}),
);
// Write the tree.
await fileTree.writeTo(destination);
await build(packageManager, destination);
}
catch (error) {
// eslint-disable-next-line no-console
console.error(error);
}
2024-01-04 18:39:21 -06:00
});
2024-01-04 03:20:55 -06:00
await program.parseAsync(process.argv);
2022-03-01 01:52:56 -06:00
})();