Skip to content
Permalink
Browse files

Added support for custom CLI config and discovery paths

You can now modify your 'cli/index.js' file to look like:

module.exports = {
  discover: [
   '../packages'
  ],
  tasks: []
};

To make the 'npm run package:discover' packages not installed or linked
with npm.
  • Loading branch information...
andersevenrud committed Aug 25, 2018
1 parent 03faa73 commit 6a61ba19c34704ae85e491becb40e61a84d28be0
Showing with 45 additions and 25 deletions.
  1. +38 −22 src/cli.js
  2. +7 −3 src/tasks/discover.js
@@ -50,36 +50,28 @@ const DEFAULT_TASKS = {
'watch:all': require('./tasks/watch.js')
};

const loadTasks = (options, args) => {
const loadTasks = (includes, options) => {
const tasks = Object.assign({}, DEFAULT_TASKS);
const logger = signale.scope('osjs-cli');

const loadFile = path.resolve(options.cli, 'index.js');
if (fs.existsSync(loadFile)) {
try {
const includes = require(loadFile);
return new Promise((resolve, reject) => {
const promises = includes.map(fn => fn(options));
const promises = includes.map(fn => fn(options));

Promise.all(promises).then(results => {
results.forEach(ts => Object.assign(tasks, ts));
resolve(tasks);
}).catch(reject);
});
} catch (e) {
logger.warn('An error occured while loading tasks');
logger.fatal(new Error(e));
}
}

return Promise.resolve(tasks);
return Promise.all(promises)
.then(results => {
return results.reduce((list, iter) => {
return Object.assign({}, list, iter);
}, tasks);
});
};

const createOptions = options => Object.assign({
production: !!(process.env.NODE_ENV || 'development').match(/^prod/),
cli: path.resolve(options.root, 'src/cli'),
npm: path.resolve(options.root, 'package.json'),
packages: path.resolve(options.root, 'packages.json'),
config: {
discover: [
path.resolve(options.root, 'node_modules')
]
},
dist: {
root: path.resolve(options.root, 'dist'),
themes: path.resolve(options.root, 'dist/themes'),
@@ -90,15 +82,39 @@ const createOptions = options => Object.assign({
}, options);

const cli = async (argv, opts) => {
const logger = signale.scope('osjs-cli');
const options = createOptions(opts);
const loadFile = path.resolve(options.cli, 'index.js');
const args = minimist(argv);
const [arg] = args._;
const error = msg => {
console.error(msg);
process.exit(1);
};

loadTasks(options, args).then(tasks => {

let tasks = [];
if (fs.existsSync(loadFile)) {
try {
const c = require(loadFile);

if (c instanceof Array) {
tasks = c;
} else {
tasks = c.tasks;

options.config.discover = [
...options.config.discover,
c.discover || []
];
}
} catch (e) {
logger.warn('An error occured while loading cli config');
logger.fatal(new Error(e));
}
}

loadTasks(tasks, options).then(tasks => {
if (!arg) {
error('Available tasks: \n' + Object.keys(tasks).map(t => `- ${t}`).join('\n'));
} else if (arg in tasks) {
@@ -35,11 +35,14 @@ const globby = require('globby');
const clean = dir => globby(dir, {deep: false, onlyDirectories: true})
.then(files => Promise.all(files.map(file => fs.unlink(file))));

const getAllPackages = dirs => Promise.all(dirs.map(dir => {
return utils.npmPackages(dir);
})).then(results => [].concat(...results));

module.exports = async ({logger, options, args}) => {
logger.await('Discovering packages');

const dir = path.resolve(options.root, 'node_modules');
const packages = await utils.npmPackages(dir);
const packages = await getAllPackages(options.config.discover);
const discovery = packages.map(pkg => pkg.filename);
const manifest = packages.map(({meta}) => meta);

@@ -55,7 +58,8 @@ module.exports = async ({logger, options, args}) => {
: path.resolve(options.dist.packages, pkg.meta.name);

return fs.ensureDir(s)
.then(() => fs.ensureSymlink(s, d, 'junction'));
.then(() => fs.ensureSymlink(s, d, 'junction'))
.catch(err => console.warn(err));
});

return Promise.resolve()

0 comments on commit 6a61ba1

Please sign in to comment.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.