Skip to content

Commit

Permalink
- skip optional dependencies that are not installed
Browse files Browse the repository at this point in the history
 - tweak message style
  • Loading branch information
zbryikt committed Jan 28, 2024
1 parent 1411bc4 commit c1b6085
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 14 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## v1.1.12 (upcoming)

- skip optional dependencies that are not installed
- tweak message style
- upgrade dependencies


Expand Down
39 changes: 32 additions & 7 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env node
var colors, fs, path, os, fsExtra, browserify, yargs, child_process, glob, cmds, slice$ = [].slice;
var colors, fs, path, os, fsExtra, browserify, yargs, child_process, glob, quit, cmds, slice$ = [].slice;
colors = require('@plotdb/colors');
fs = require('fs');
path = require('path');
Expand All @@ -9,6 +9,10 @@ browserify = require('browserify');
yargs = require('yargs');
child_process = require('child_process');
glob = require('glob');
quit = function(){
console.error(Array.from(arguments).join(''));
return process.exit();
};
cmds = {};
cmds['default'] = {
command: '$0',
Expand All @@ -27,7 +31,7 @@ cmds['default'] = {
});
},
handler: function(argv){
var ret, localModules, useSymlink, fed, extModules;
var ret, localModules, useSymlink, pkg, fed, extModules, skips, allPromises;
if (argv.l) {
ret = argv.l.split(';').map(function(it){
return it.split(':');
Expand All @@ -42,25 +46,28 @@ cmds['default'] = {
localModules = [];
}
useSymlink = argv.s != null ? argv.s : true;
pkg = JSON.parse(fs.readFileSync("package.json").toString());
fed = import$({
root: '.',
modules: []
}, JSON.parse(fs.readFileSync("package.json").toString()).frontendDependencies || {});
}, pkg.frontendDependencies || {});
extModules = localModules.filter(function(o){
return !fed.modules.filter(function(it){
return it === o.name;
}).length;
});
if (extModules.length) {
console.warn("following modules are not listed in fedep modules. still installed:".yellow);
console.warn("[WARN] following modules are not listed in fedep modules. still installed:".yellow);
console.warn(extModules.map(function(it){
return " - " + it.name;
}).join('\n').yellow);
console.warn("");
fed.modules = fed.modules.concat(extModules.map(function(it){
return it.name;
}));
}
return (fed.modules || []).map(function(obj){
skips = [];
allPromises = (fed.modules || []).map(function(obj){
var localModule, root, base, info, id, mainFile, ref$, i$, name, version, ret, that, desdir, maindir, p, srcdir, realSrcdir, srcFile, desFile;
obj = typeof obj === 'string' ? {
name: obj
Expand All @@ -84,6 +91,16 @@ cmds['default'] = {
}
root = path.relative('.', root);
}
if (!fs.existsSync(path.join(root, 'package.json'))) {
if (!(pkg.optionalDependencies || {})[obj.name]) {
quit(" -- [ERROR] Module `".red, obj.name.brightYellow, "` is not found. Failed.".red);
}
skips.push({
name: obj.name,
reason: "it is an optional dependency and is not installed"
});
return console.warn([" -- [WARN] Optional dependencies `".yellow, obj.name.brightYellow, "` is not found. Skipped.".yellow].join(''));
}
info = JSON.parse(fs.readFileSync(path.join(root, "package.json")).toString());
id = info._id || info.name + "@" + info.version;
mainFile = {
Expand All @@ -95,7 +112,7 @@ cmds['default'] = {
})[0]
};
if (/\.\.|^\//.exec(id)) {
throw new Error("fedep: not supported name in module " + obj.name + ".");
quit((" -- [ERROR] not supported id `" + id + "` in module `").red, obj.name.brightYellow, "`.".red);
}
ref$ = id.split("@"), name = 0 < (i$ = ref$.length - 1) ? slice$.call(ref$, 0, i$) : (i$ = 0, []), version = ref$[i$];
if (ret = /#([a-zA-Z0-9_.-]+)$/.exec(version)) {
Expand Down Expand Up @@ -197,6 +214,14 @@ cmds['default'] = {
}
});
});
return Promise.all(allPromises).then(function(){
if (skips.length) {
console.warn(("[WARN] Skipped module" + (skips.length > 1 ? 's' : '') + ":").yellow);
return skips.map(function(s){
return console.warn(" -", s.name.brightYellow, s.reason ? ("(" + s.reason + ")").gray : '');
});
}
});
}
};
cmds.init = {
Expand Down Expand Up @@ -253,7 +278,7 @@ cmds.publish = {
fsExtra.removeSync(workFolder);
}
if (!fs.existsSync(srcFolder)) {
console.error("specified publish folder `" + srcFolder + "` doesn't exist. exit.");
console.error("[ERROR] specified publish folder `".red + srcFolder.brightYellow + "` doesn't exist. exit.".red);
process.exit();
}
fsExtra.ensureDirSync(workFolder);
Expand Down
31 changes: 24 additions & 7 deletions lib/main.ls
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/usr/bin/env node
require! <[@plotdb/colors fs path os fs-extra browserify yargs child_process glob]>

quit = -> console.error(Array.from(arguments).join('')); process.exit!

cmds = {}
cmds.default =
command: \$0
Expand All @@ -26,17 +28,18 @@ cmds.default =

use-symlink = if argv.s? => argv.s else true

fed = {
root: '.', modules: []
} <<< (JSON.parse(fs.read-file-sync "package.json" .toString!).frontendDependencies or {})
pkg = JSON.parse(fs.read-file-sync "package.json" .toString!)
fed = {root: '.', modules: []} <<< (pkg.frontendDependencies or {})

ext-modules = local-modules.filter((o) -> !(fed.modules.filter(->it == o.name).length))
if ext-modules.length =>
console.warn "following modules are not listed in fedep modules. still installed:".yellow
console.warn "[WARN] following modules are not listed in fedep modules. still installed:".yellow
console.warn ext-modules.map(->" - #{it.name}").join(\\n).yellow
console.warn ""
fed.modules ++= ext-modules.map(-> it.name)
(fed.modules or []).map (obj) ->
skips = []
all-promises = (fed.modules or []).map (obj) ->
obj = if typeof(obj) == \string => {name: obj} else obj
local-module = local-modules.filter(-> it.name == obj.name).0
if local-modules.length and !local-module => return
Expand All @@ -49,14 +52,24 @@ cmds.default =
base = path.join(base, \..)
root = path.relative('.', root)
if !fs.exists-sync(path.join root, \package.json) =>
if !(pkg.optionalDependencies or {})[obj.name] =>
quit " -- [ERROR] Module `".red, obj.name.brightYellow, "` is not found. Failed.".red
skips.push(name: obj.name, reason: "it is an optional dependency and is not installed")
return console.warn [
" -- [WARN] Optional dependencies `".yellow, obj.name.brightYellow, "` is not found. Skipped.".yellow
].join('')
info = JSON.parse(fs.read-file-sync path.join(root, "package.json") .toString!)
id = info._id or "#{info.name}@#{info.version}"
main-file =
js: [info.browser, info.main].filter(-> /\.js/.exec(it)).0
css: [info.style, info.browser, info.main].filter(-> /\.css/.exec(it)).0
if /\.\.|^\//.exec(id) => throw new Error("fedep: not supported name in module #{obj.name}.")
if /\.\.|^\//.exec(id) =>
quit " -- [ERROR] not supported id `#{id}` in module `".red, obj.name.brightYellow, "`.".red
[...name,version] = id.split("@")
# TODO we may need a better approach for version resolving if semver cannot be found.
Expand Down Expand Up @@ -133,6 +146,10 @@ cmds.default =
if use-symlink => fs-extra.ensure-symlink-sync desdir, maindir
else if fs.lstat-sync desdir .is-symbolic-link! => fs-extra.copy-sync srcdir, maindir
else fs-extra.copy-sync desdir, maindir
Promise.all all-promises .then ->
if skips.length =>
console.warn "[WARN] Skipped module#{if skips.length > 1 => \s else ''}:".yellow
skips.map (s) -> console.warn " -", s.name.brightYellow, if s.reason => "(#{s.reason})".gray else ''
cmds.init =
Expand Down Expand Up @@ -167,7 +184,7 @@ cmds.publish =
work-folder = ".fedep/publish"
if fs.exists-sync work-folder => fs-extra.remove-sync work-folder
if !fs.exists-sync(src-folder) =>
console.error "specified publish folder `#{src-folder}` doesn't exist. exit."
console.error "[ERROR] specified publish folder `".red + src-folder.brightYellow + "` doesn't exist. exit.".red
process.exit!
fs-extra.ensure-dir-sync work-folder
Expand Down

0 comments on commit c1b6085

Please sign in to comment.