feat: flecks add

This commit is contained in:
cha0s 2024-01-03 19:23:11 -06:00
parent 77a5c60a44
commit f44027854d
4 changed files with 36 additions and 8 deletions

View File

@ -32,7 +32,7 @@
- [ ] `$flecks/db/sequelize` should be `$flecks/db.sequelize`
- [x] `url()` in styles breaks HMR
- [ ] 2 underscores for `FLECKS_ENV` variables
- [ ] flecks add
- [x] flecks add
# Next

View File

@ -7,6 +7,7 @@ import flatten from 'lodash.flatten';
import rimraf from 'rimraf';
import D from '../debug';
import Flecks from './flecks';
const {
FLECKS_CORE_ROOT = process.cwd(),
@ -45,6 +46,26 @@ export const spawnWith = (cmd, opts = {}) => {
export default (program, flecks) => {
const commands = {
add: {
args: [
new Argument('<fleck>>', 'fleck'),
],
description: 'add a fleck to your application',
action: async (fleck, opts) => {
const {
noYarn,
} = opts;
await processCode(
noYarn
? spawn('npm', ['install', fleck], {stdio: 'inherit'})
: spawn('yarn', ['add', fleck], {stdio: 'inherit'}),
);
await Flecks.addFleckToYml(fleck);
},
options: [
['--no-yarn', 'use npm instead of yarn'],
],
},
clean: {
description: 'remove node_modules, lock file, build artifacts, then reinstall',
action: (opts) => {

View File

@ -3,6 +3,7 @@ import {
realpathSync,
statSync,
} from 'fs';
import {readFile, writeFile} from 'fs/promises';
import {
basename,
dirname,
@ -10,10 +11,12 @@ import {
isAbsolute,
join,
resolve,
sep,
} from 'path';
import babelmerge from 'babel-merge';
import enhancedResolve from 'enhanced-resolve';
import {dump as dumpYml, load as loadYml} from 'js-yaml';
import {addHook} from 'pirates';
import D from '../debug';
@ -40,6 +43,14 @@ export default class ServerFlecks extends Flecks {
this.rcs = options.rcs || {};
}
static async addFleckToYml(fleck, path) {
const key = [fleck].concat(path ? `.${sep}${join('packages', path, 'src')}` : []).join(':');
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, {sortKeys: true}));
}
get aliasedConfig() {
const aliases = this.aliases();
return Object.fromEntries(

View File

@ -1,8 +1,8 @@
import {readFile, stat, writeFile} from 'fs/promises';
import {stat} from 'fs/promises';
import {join, normalize} from 'path';
import {build, move, validate} from '@flecks/create-app/server';
import {dumpYml, Flecks, loadYml} from '@flecks/core/server';
import {Flecks} from '@flecks/core/server';
import {confirm} from '@inquirer/prompts';
const {
@ -65,11 +65,7 @@ const create = async (flecks) => {
await move(name, join(__dirname, 'template'), destination, 'fleck', flecks);
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}));
await Flecks.addFleckToYml(name, pkg);
}
};