2024-01-03 17:16:54 -06:00
|
|
|
import {readFile, stat, writeFile} from 'fs/promises';
|
2022-02-25 04:58:08 -06:00
|
|
|
import {join, normalize} from 'path';
|
|
|
|
|
2022-03-01 01:52:56 -06:00
|
|
|
import {build, move, validate} from '@flecks/create-app/server';
|
2024-01-03 17:16:54 -06:00
|
|
|
import {dumpYml, Flecks, loadYml} from '@flecks/core/server';
|
|
|
|
import {confirm} from '@inquirer/prompts';
|
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;
|
|
|
|
|
2022-02-28 05:16:24 -06:00
|
|
|
const cwd = normalize(FLECKS_CORE_ROOT);
|
2022-02-25 04:58:08 -06:00
|
|
|
|
2024-01-03 17:16:54 -06:00
|
|
|
const checkIsMonorepo = async (cwd) => {
|
2022-02-25 04:58:08 -06:00
|
|
|
try {
|
2022-03-01 01:52:56 -06:00
|
|
|
await stat(join(cwd, 'packages'));
|
|
|
|
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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-03-01 01:52:56 -06:00
|
|
|
const monorepoScope = async (cwd) => {
|
2022-02-25 04:58:08 -06:00
|
|
|
try {
|
2022-03-01 01:52:56 -06:00
|
|
|
const {name} = __non_webpack_require__(join(cwd, 'package.json'));
|
|
|
|
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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-03-01 01:52:56 -06:00
|
|
|
const target = async (name) => {
|
|
|
|
const {errors} = validate(name);
|
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
|
|
|
}
|
2022-03-01 01:52:56 -06:00
|
|
|
const parts = name.split('/');
|
2022-02-25 04:58:08 -06:00
|
|
|
let pkg;
|
|
|
|
let scope;
|
|
|
|
if (1 === parts.length) {
|
2022-03-01 01:52:56 -06:00
|
|
|
pkg = name;
|
2024-01-03 17:16:54 -06:00
|
|
|
if (await checkIsMonorepo(cwd)) {
|
2022-03-01 01:52:56 -06:00
|
|
|
scope = await monorepoScope(cwd);
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
const create = async (flecks) => {
|
2024-01-03 17:16:54 -06:00
|
|
|
const isMonorepo = await checkIsMonorepo(cwd);
|
2022-03-01 01:52:56 -06:00
|
|
|
const [scope, pkg] = await target(process.argv[2]);
|
2024-01-03 17:16:54 -06:00
|
|
|
const path = scope && isMonorepo ? join(cwd, 'packages') : cwd;
|
2022-02-25 04:58:08 -06:00
|
|
|
const name = [scope, pkg].filter((e) => !!e).join('/');
|
|
|
|
const destination = join(path, pkg);
|
2022-03-01 10:30:33 -06:00
|
|
|
await move(name, join(__dirname, 'template'), destination, 'fleck', flecks);
|
2022-03-01 01:52:56 -06:00
|
|
|
await build(destination);
|
2024-01-03 17:16:54 -06:00
|
|
|
if (isMonorepo && await confirm({message: 'Add fleck to `build/flecks.yml`?'})) {
|
|
|
|
const key = `${name}:${join('.', 'packages', pkg)}`;
|
|
|
|
const ymlPath = join(FLECKS_CORE_ROOT, 'build', 'flecks.yml');
|
|
|
|
let yml = loadYml(await readFile(ymlPath));
|
|
|
|
yml = Object.fromEntries(Object.entries(yml).concat([[key, {}]]));
|
|
|
|
await writeFile(ymlPath, dumpYml(yml, {forceQuotes: true, sortKeys: true}));
|
|
|
|
}
|
2022-02-25 04:58:08 -06:00
|
|
|
};
|
|
|
|
|
2022-03-01 01:52:56 -06:00
|
|
|
(async () => {
|
|
|
|
const flecks = await Flecks.bootstrap();
|
|
|
|
try {
|
|
|
|
await create(flecks);
|
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.error(error.message);
|
|
|
|
process.exitCode = 1;
|
|
|
|
}
|
|
|
|
})();
|