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

99 lines
2.8 KiB
JavaScript
Raw Normal View History

2024-01-16 00:28:20 -06:00
#!/usr/bin/env node
2024-02-09 01:11:27 -06:00
/* eslint-disable camelcase */
2022-02-25 04:58:08 -06:00
2024-02-09 01:11:27 -06:00
const {
basename,
dirname,
join,
relative,
sep,
} = require('path');
2024-01-16 00:28:20 -06:00
2024-02-09 01:11:27 -06:00
const addPathsToYml = require('@flecks/core/build/add-paths-to-yml');
2024-02-01 14:02:57 -06:00
const {program} = require('commander');
2024-02-09 01:11:27 -06:00
const {move} = require('@flecks/core/build/move');
2024-02-01 14:02:57 -06:00
const {
build,
install,
2024-02-09 01:11:27 -06:00
JsonStream,
} = require('@flecks/core/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-02-09 01:11:27 -06:00
const {
npm_config_local_prefix,
npm_config_scope,
npm_package_json,
} = process.env;
2022-03-01 01:52:56 -06:00
(async () => {
2024-02-09 01:11:27 -06:00
program.argument('[path]', "the path of the fleck (e.g.: 'packages/foobar')");
2024-01-04 03:20:55 -06:00
program.option('--no-add', 'do not add an entry to `build/flecks.yml`');
2024-02-09 01:11:27 -06:00
program.option('--no-inherit-version', 'do not inherit root package version');
program.option('--alias', 'alias the fleck in `build/flecks.yml`');
program.option('--dry-run', 'just say what would be done without actually doing it');
program.action(async (
path,
{
alias,
add,
inheritVersion,
packageManager,
},
) => {
2024-01-04 03:20:55 -06:00
try {
2024-02-09 01:11:27 -06:00
if (!npm_config_local_prefix && !path) {
throw new Error('name required');
}
const root = npm_config_local_prefix || FLECKS_CORE_ROOT;
let rootJson;
try {
rootJson = require(join(root, 'package.json'));
}
// eslint-disable-next-line no-empty
catch (error) {}
let scope;
if (npm_config_scope) {
scope = npm_config_scope;
}
else if (rootJson?.name) {
const inferredScope = rootJson.name.split('/')[0] || '';
if (inferredScope.startsWith('@')) {
scope = inferredScope;
}
2024-01-04 03:20:55 -06:00
}
2024-02-09 01:11:27 -06:00
const local = basename(path || root);
const name = scope ? `${scope}/${local}` : local;
2024-01-20 04:59:13 -06:00
const fileTree = await move(name, join(__dirname, '..', 'template'));
2024-02-09 01:11:27 -06:00
if (inheritVersion && rootJson?.version) {
2024-01-21 06:56:53 -06:00
// Inherit version from monorepo root.
2024-02-09 01:11:27 -06:00
const {version} = rootJson;
fileTree.pipe('package.json', new JsonStream((json) => ({...json, version})));
2024-01-21 06:56:53 -06:00
}
2024-01-04 03:20:55 -06:00
// Write the tree.
2024-02-09 01:11:27 -06:00
const destination = path ? join(root, path) : dirname(npm_package_json);
2024-01-04 03:20:55 -06:00
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-02-09 01:11:27 -06:00
if (add) {
const maybeAliasedPath = [name]
.concat(alias ? `.${sep}${relative(root, destination)}` : [])
.join(':');
try {
await addPathsToYml([maybeAliasedPath], root);
}
// eslint-disable-next-line no-empty
catch (error) {}
2024-01-04 03:20:55 -06:00
}
}
catch (error) {
// eslint-disable-next-line no-console
2024-02-09 01:11:27 -06:00
console.error('creation failed:', error);
2024-01-04 03:20:55 -06:00
}
});
await program.parseAsync(process.argv);
2022-03-01 01:52:56 -06:00
})();