2024-01-04 03:20:55 -06:00
|
|
|
import {join} from 'path';
|
2022-02-25 04:58:08 -06:00
|
|
|
|
2024-01-04 03:20:55 -06:00
|
|
|
import {
|
|
|
|
dumpYml,
|
|
|
|
Flecks,
|
|
|
|
loadYml,
|
|
|
|
Option,
|
|
|
|
program,
|
|
|
|
transform,
|
|
|
|
} from '@flecks/core/server';
|
2022-03-01 01:52:56 -06:00
|
|
|
import validate from 'validate-npm-package-name';
|
2022-02-25 04:58:08 -06:00
|
|
|
|
2022-03-01 01:52:56 -06:00
|
|
|
import build from './build';
|
2024-01-04 03:20:55 -06:00
|
|
|
import move, {testDestination} from './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(
|
|
|
|
new Option('--package-manager <binary>', 'package manager binary')
|
|
|
|
.choices(['npm', 'bun', 'yarn'])
|
|
|
|
.default('npm'),
|
|
|
|
);
|
|
|
|
program.action(async (app, {packageManager}) => {
|
|
|
|
const flecks = await Flecks.bootstrap({
|
|
|
|
config: {
|
|
|
|
'@flecks/core': {},
|
|
|
|
'@flecks/core/server': {packageManager},
|
|
|
|
'@flecks/create-app': {},
|
|
|
|
'@flecks/fleck': {},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
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-04 18:39:21 -06:00
|
|
|
const fileTree = await move(name, join(__dirname, 'template'), 'app', flecks);
|
2024-01-04 03:20:55 -06:00
|
|
|
fileTree.pipe(
|
|
|
|
'build/flecks.yml',
|
|
|
|
transform((chunk, encoding, done, stream) => {
|
|
|
|
const yml = loadYml(chunk);
|
2024-01-04 18:39:21 -06:00
|
|
|
if ('npm' !== packageManager) {
|
|
|
|
yml['@flecks/core/server'] = {packageManager};
|
|
|
|
}
|
|
|
|
yml['@flecks/core'] = {id: app};
|
|
|
|
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
|
|
|
})();
|