Skip to content

Commit

Permalink
feat: package manager (and bun)
Browse files Browse the repository at this point in the history
  • Loading branch information
cha0s committed Jan 4, 2024
1 parent 7198ad9 commit 16934d5
Show file tree
Hide file tree
Showing 10 changed files with 159 additions and 109 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ dist

# local
/yarn.lock
/bun.lockb

# package-locals
/packages/*/yarn.lock
/packages/*/bun.lockb
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
},
"scripts": {
"build": "NODE_PATH=./node_modules webpack --config ./build/webpack.config.js --mode production",
"clean": "rm -rf dist node_modules yarn.lock && yarn",
"clean": "rm -rf dist bun.lockb && bun install",
"lint": "NODE_PATH=./node_modules eslint --config ./build/eslint.config.js .",
"postversion": "cp package.json dist",
"test": "npm run build && mocha -t 10000 --colors ./dist/test.js"
Expand Down
49 changes: 25 additions & 24 deletions packages/core/src/server/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const debug = D('@flecks/core/commands');
const debugSilly = debug.extend('silly');
const flecksRoot = normalize(FLECKS_CORE_ROOT);

export {Argument};
export {Argument, Option, program} from 'commander';

export const processCode = (child) => new Promise((resolve, reject) => {
child.on('error', reject);
Expand Down Expand Up @@ -45,45 +45,46 @@ export const spawnWith = (cmd, opts = {}) => {
};

export default (program, flecks) => {
const {packageManager} = flecks.get('@flecks/core/server');
const commands = {
add: {
args: [
new Argument('<fleck>>', 'fleck'),
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'}),
);
const args = [];
if ('yarn' === packageManager) {
args.push('yarn', ['add', fleck]);
}
else {
args.push(packageManager, ['install', fleck]);
}
args.push({stdio: 'inherit'});
await processCode(spawn(...args));
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) => {
const {
noYarn,
} = opts;
rimraf.sync(join(flecksRoot, 'dist'));
rimraf.sync(join(flecksRoot, 'node_modules'));
if (noYarn) {
rimraf.sync(join(flecksRoot, 'package-lock.json'));
return spawn('npm', ['install'], {stdio: 'inherit'});
switch (packageManager) {
case 'yarn':
rimraf.sync(join(flecksRoot, 'yarn.lock'));
break;
case 'bun':
rimraf.sync(join(flecksRoot, 'bun.lockb'));
break;
case 'npm':
rimraf.sync(join(flecksRoot, 'package-lock.json'));
break;
default:
break;
}
rimraf.sync(join(flecksRoot, 'yarn.lock'));
return spawn('yarn', [], {stdio: 'inherit'});
return spawn(packageManager, ['install'], {stdio: 'inherit'});
},
options: [
['--no-yarn', 'use npm instead of yarn'],
],
},
};
const targets = flatten(flecks.invokeFlat('@flecks/core.targets'));
Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ export {dump as dumpYml, load as loadYml} from 'js-yaml';
export {
Argument,
default as commands,
Option,
processCode,
program,
spawnWith,
} from './commands';
export {default as Flecks} from './flecks';
Expand Down Expand Up @@ -101,6 +103,10 @@ export const hooks = {
* Build targets to exclude from ESLint.
*/
'eslint.exclude': [],
/**
* The package manager used for tasks.
*/
packageManager: 'npm',
/**
* Build targets to profile with `webpack.debug.ProfilingPlugin`.
*/
Expand Down
6 changes: 3 additions & 3 deletions packages/create-app/src/build.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {processCode, spawnWith} from '@flecks/core/server';

export default async (cwd) => {
const code = await processCode(spawnWith(['yarn'], {cwd}));
export default async (packageManager, cwd) => {
const code = await processCode(spawnWith([packageManager, 'install'], {cwd}));
if (0 !== code) {
return code;
}
return processCode(spawnWith(['yarn', 'build'], {cwd}));
return processCode(spawnWith([packageManager, 'run', 'build'], {cwd}));
};
89 changes: 61 additions & 28 deletions packages/create-app/src/cli.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,72 @@
import {join, normalize} from 'path';
import {join} from 'path';

import {Flecks} from '@flecks/core/server';
import {
dumpYml,
Flecks,
loadYml,
Option,
program,
transform,
} from '@flecks/core/server';
import validate from 'validate-npm-package-name';

import build from './build';
import move from './move';
import move, {testDestination} from './move';

const {
FLECKS_CORE_ROOT = process.cwd(),
} = process.env;

const cwd = normalize(FLECKS_CORE_ROOT);

const create = async (flecks) => {
let name = process.argv[2];
const {errors} = validate(name);
if (errors) {
throw new Error(`@flecks/create-app: invalid app name: ${errors.join(', ')}`);
}
const destination = join(cwd, name);
if (!name.startsWith('@')) {
name = `@${name}/monorepo`;
}
await move(name, join(__dirname, 'template'), destination, 'app', flecks);
await build(destination);
};

(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;
}
program.argument('<app>', 'name of the app to create');
program.addOption(
new Option('--package-manager <binary>', 'package manager binary')
.choices(['npm', 'bun', 'yarn'])
.default('npm'),
);
program.action(async (app, {packageManager}) => {
const flecks = await Flecks.bootstrap({
config: {
'@flecks/core': {},
'@flecks/core/server': {packageManager},
'@flecks/create-app': {},
'@flecks/fleck': {},
},
});
try {
const {errors} = validate(app);
if (errors) {
throw new Error(`@flecks/create-app: invalid app name: ${errors.join(', ')}`);
}
const destination = join(FLECKS_CORE_ROOT, app);
if (!app.startsWith('@')) {
app = `@${app}/monorepo`;
}
if (!await testDestination(destination)) {
const error = new Error(
`@flecks/create-app: destination '${destination} already exists: aborting`,
);
error.code = 129;
throw error;
}
const fileTree = await move(app, join(__dirname, 'template'), 'app', flecks);
fileTree.pipe(
'build/flecks.yml',
transform((chunk, encoding, done, stream) => {
const yml = loadYml(chunk);
yml['@flecks/core/server'] = {packageManager};
stream.push(dumpYml(yml, {sortKeys: true}));
done();
}),
);
// Write the tree.
await fileTree.writeTo(destination);
await build(packageManager, destination);
}
catch (error) {
// eslint-disable-next-line no-console
console.error(error);
}
});
await program.parseAsync(process.argv);
})();
19 changes: 7 additions & 12 deletions packages/create-app/src/move.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ import {
} from 'fs/promises';
import {basename, dirname, join} from 'path';

import {JsonStream, transform} from '@flecks/core/server';
import {
JsonStream,
transform,
} from '@flecks/core/server';

import FileTree from './tree';

const testDestination = async (destination) => {
export const testDestination = async (destination) => {
try {
await stat(destination);
return false;
Expand All @@ -20,14 +23,7 @@ const testDestination = async (destination) => {
}
};

export default async (name, source, destination, type, flecks) => {
if (!await testDestination(destination)) {
const error = new Error(
`@flecks/create-fleck: destination '${destination} already exists: aborting`,
);
error.code = 129;
throw error;
}
export default async (name, source, type, flecks) => {
const fileTree = await FileTree.loadFrom(source);
// Renamed to avoid conflicts.
const {files} = fileTree;
Expand Down Expand Up @@ -56,6 +52,5 @@ export default async (name, source, destination, type, flecks) => {
.forEach((path) => {
fileTree.pipe(path, new JsonStream.PrettyPrint());
});
// Write the tree.
await fileTree.writeTo(destination);
return fileTree;
};
2 changes: 1 addition & 1 deletion packages/create-app/src/server.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export {default as validate} from 'validate-npm-package-name';

export {default as build} from './build';
export {default as move} from './move';
export {default as move, testDestination} from './move';
export {default as FileTree} from './tree';
3 changes: 1 addition & 2 deletions packages/create-fleck/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
],
"dependencies": {
"@flecks/core": "^2.0.3",
"@flecks/create-app": "^2.0.3",
"@inquirer/prompts": "^3.3.0"
"@flecks/create-app": "^2.0.3"
},
"devDependencies": {
"@flecks/fleck": "^2.0.3"
Expand Down
Loading

0 comments on commit 16934d5

Please sign in to comment.