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

117 lines
3.3 KiB
JavaScript
Raw Normal View History

2024-01-16 00:28:20 -06:00
#!/usr/bin/env node
2022-02-25 04:58:08 -06:00
2024-01-16 00:28:20 -06:00
const {stat} = require('fs/promises');
const {join} = require('path');
2024-02-01 14:02:57 -06:00
const addPathsToYml = require('@flecks/build/build/add-paths-to-yml');
const {program} = require('commander');
const {
build,
install,
transform,
} = require('@flecks/core/server');
2024-01-20 04:59:13 -06:00
const {move, testDestination} = require('@flecks/create-app/build/move');
2024-02-03 20:31:35 -06:00
const {validate} = require('@flecks/create-app/src/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-20 04:59:13 -06:00
const {name} = 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`');
2024-01-04 15:15:02 -06:00
program.option('--no-alias', 'do not alias the fleck in `build/flecks.yml`');
2024-02-01 14:02:57 -06:00
program.addOption(
program.createOption('-pm,--package-manager <binary>', 'package manager binary')
.choices(['npm', 'bun', 'pnpm', 'yarn']),
);
program.action(async (fleck, {alias, add, packageManager}) => {
2024-01-04 03:20:55 -06:00
try {
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;
}
2024-01-20 04:59:13 -06:00
const fileTree = await move(name, join(__dirname, '..', 'template'));
2024-01-21 06:56:53 -06:00
if (isMonorepo) {
const {version} = require(join(FLECKS_CORE_ROOT, 'package.json'));
// Inherit version from monorepo root.
fileTree.pipe(
'package.json',
transform((chunk, encoding, done, stream) => {
stream.push(JSON.stringify({...JSON.parse(chunk), version}));
done();
}),
);
}
2024-01-04 03:20:55 -06:00
// Write the tree.
await fileTree.writeTo(destination);
2024-02-01 14:02:57 -06:00
// Install and build.
await install({cwd: destination, packageManager});
await build({cwd: destination, packageManager});
2024-01-04 03:20:55 -06:00
if (isMonorepo && add) {
2024-02-01 14:02:57 -06:00
await addPathsToYml([[name].concat(alias ? `./packages/${pkg}` : []).join(':')]);
2024-01-04 03:20:55 -06:00
}
}
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
})();