feat: add to build/flecks.yml

This commit is contained in:
cha0s 2024-01-03 17:16:54 -06:00
parent b74e23ccc0
commit 77a5c60a44
2 changed files with 16 additions and 6 deletions

View File

@ -27,7 +27,8 @@
], ],
"dependencies": { "dependencies": {
"@flecks/core": "^2.0.3", "@flecks/core": "^2.0.3",
"@flecks/create-app": "^2.0.3" "@flecks/create-app": "^2.0.3",
"@inquirer/prompts": "^3.3.0"
}, },
"devDependencies": { "devDependencies": {
"@flecks/fleck": "^2.0.3" "@flecks/fleck": "^2.0.3"

View File

@ -1,8 +1,9 @@
import {stat} from 'fs/promises'; import {readFile, stat, writeFile} from 'fs/promises';
import {join, normalize} from 'path'; import {join, normalize} from 'path';
import {build, move, validate} from '@flecks/create-app/server'; import {build, move, validate} from '@flecks/create-app/server';
import {Flecks} from '@flecks/core/server'; import {dumpYml, Flecks, loadYml} from '@flecks/core/server';
import {confirm} from '@inquirer/prompts';
const { const {
FLECKS_CORE_ROOT = process.cwd(), FLECKS_CORE_ROOT = process.cwd(),
@ -10,7 +11,7 @@ const {
const cwd = normalize(FLECKS_CORE_ROOT); const cwd = normalize(FLECKS_CORE_ROOT);
const hasPackages = async (cwd) => { const checkIsMonorepo = async (cwd) => {
try { try {
await stat(join(cwd, 'packages')); await stat(join(cwd, 'packages'));
return true; return true;
@ -47,7 +48,7 @@ const target = async (name) => {
let scope; let scope;
if (1 === parts.length) { if (1 === parts.length) {
pkg = name; pkg = name;
if (await hasPackages(cwd)) { if (await checkIsMonorepo(cwd)) {
scope = await monorepoScope(cwd); scope = await monorepoScope(cwd);
} }
return [scope, pkg]; return [scope, pkg];
@ -56,12 +57,20 @@ const target = async (name) => {
}; };
const create = async (flecks) => { const create = async (flecks) => {
const isMonorepo = await checkIsMonorepo(cwd);
const [scope, pkg] = await target(process.argv[2]); const [scope, pkg] = await target(process.argv[2]);
const path = scope && (await hasPackages(cwd)) ? join(cwd, 'packages') : cwd; const path = scope && isMonorepo ? join(cwd, 'packages') : cwd;
const name = [scope, pkg].filter((e) => !!e).join('/'); const name = [scope, pkg].filter((e) => !!e).join('/');
const destination = join(path, pkg); const destination = join(path, pkg);
await move(name, join(__dirname, 'template'), destination, 'fleck', flecks); await move(name, join(__dirname, 'template'), destination, 'fleck', flecks);
await build(destination); await build(destination);
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}));
}
}; };
(async () => { (async () => {