2024-01-03 19:23:11 -06:00
|
|
|
import {stat} from 'fs/promises';
|
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 {
|
|
|
|
build,
|
|
|
|
move,
|
|
|
|
testDestination,
|
|
|
|
validate,
|
|
|
|
} from '@flecks/create-app/server';
|
|
|
|
import {Flecks, program} from '@flecks/core/server';
|
2022-02-25 04:58:08 -06:00
|
|
|
|
|
|
|
const {
|
2022-02-28 05:16:24 -06:00
|
|
|
FLECKS_CORE_ROOT = process.cwd(),
|
2022-02-25 04:58:08 -06:00
|
|
|
} = process.env;
|
|
|
|
|
2024-01-04 03:20:55 -06:00
|
|
|
const checkIsMonorepo = async () => {
|
2022-02-25 04:58:08 -06:00
|
|
|
try {
|
2024-01-04 03:20:55 -06:00
|
|
|
await stat(join(FLECKS_CORE_ROOT, 'packages'));
|
2022-03-01 01:52:56 -06:00
|
|
|
return true;
|
2022-02-25 04:58:08 -06:00
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
if ('ENOENT' !== error.code) {
|
|
|
|
throw error;
|
|
|
|
}
|
2022-03-01 01:52:56 -06:00
|
|
|
return false;
|
2022-02-25 04:58:08 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-01-04 03:20:55 -06:00
|
|
|
const monorepoScope = async () => {
|
2022-02-25 04:58:08 -06:00
|
|
|
try {
|
2024-01-04 03:20:55 -06:00
|
|
|
const {name} = __non_webpack_require__(join(FLECKS_CORE_ROOT, 'package.json'));
|
2022-03-01 01:52:56 -06:00
|
|
|
const [scope] = name.split('/');
|
|
|
|
return scope;
|
2022-02-25 04:58:08 -06:00
|
|
|
}
|
|
|
|
catch (error) {
|
2022-03-01 01:52:56 -06:00
|
|
|
if ('MODULE_NOT_FOUND' !== error.code) {
|
2022-02-25 04:58:08 -06:00
|
|
|
throw error;
|
|
|
|
}
|
2022-03-01 01:52:56 -06:00
|
|
|
return undefined;
|
2022-02-25 04:58:08 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-01-04 03:20:55 -06:00
|
|
|
const target = async (fleck) => {
|
|
|
|
const {errors} = validate(fleck);
|
2022-02-25 04:58:08 -06:00
|
|
|
if (errors) {
|
2022-03-01 01:52:56 -06:00
|
|
|
throw new Error(`@flecks/create-fleck: invalid fleck name: ${errors.join(', ')}`);
|
2022-02-25 04:58:08 -06:00
|
|
|
}
|
2024-01-04 03:20:55 -06:00
|
|
|
const parts = fleck.split('/');
|
2022-02-25 04:58:08 -06:00
|
|
|
let pkg;
|
|
|
|
let scope;
|
|
|
|
if (1 === parts.length) {
|
2024-01-04 03:20:55 -06:00
|
|
|
pkg = fleck;
|
|
|
|
if (await checkIsMonorepo()) {
|
|
|
|
scope = await monorepoScope();
|
2022-02-25 04:58:08 -06:00
|
|
|
}
|
2022-03-01 01:52:56 -06:00
|
|
|
return [scope, pkg];
|
2022-02-25 04:58:08 -06:00
|
|
|
}
|
2022-03-01 01:52:56 -06:00
|
|
|
return parts;
|
|
|
|
};
|
|
|
|
|
|
|
|
(async () => {
|
2024-01-04 03:20:55 -06:00
|
|
|
program.argument('<fleck>', 'name of the fleck to create');
|
|
|
|
program.option('--no-add', 'do not add an entry to `build/flecks.yml`');
|
|
|
|
program.action(async (fleck, {add}) => {
|
|
|
|
try {
|
|
|
|
const flecks = await Flecks.bootstrap();
|
|
|
|
const {packageManager} = flecks.get('@flecks/core/server');
|
|
|
|
const isMonorepo = await checkIsMonorepo();
|
|
|
|
const [scope, pkg] = await target(fleck);
|
|
|
|
const name = [scope, pkg].filter((e) => !!e).join('/');
|
|
|
|
const destination = join(
|
|
|
|
join(...[FLECKS_CORE_ROOT].concat(isMonorepo ? ['packages'] : [])),
|
|
|
|
pkg,
|
|
|
|
);
|
|
|
|
if (!await testDestination(destination)) {
|
|
|
|
const error = new Error(
|
|
|
|
`@flecks/create-fleck: destination '${destination} already exists: aborting`,
|
|
|
|
);
|
|
|
|
error.code = 129;
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
const fileTree = await move(name, join(__dirname, 'template'), 'fleck', flecks);
|
|
|
|
// Write the tree.
|
|
|
|
await fileTree.writeTo(destination);
|
|
|
|
await build(packageManager, destination);
|
|
|
|
if (isMonorepo && add) {
|
|
|
|
await Flecks.addFleckToYml(name, pkg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.error('Creation failed:', error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
await program.parseAsync(process.argv);
|
2022-03-01 01:52:56 -06:00
|
|
|
})();
|