Skip to content

Commit

Permalink
- support transpilation on demand.
Browse files Browse the repository at this point in the history
 - bump version
  • Loading branch information
zbryikt committed Feb 15, 2024
1 parent e6ae109 commit b1c8b92
Show file tree
Hide file tree
Showing 6 changed files with 6,520 additions and 3,078 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Change Logs

## v1.2.0

- support transpilation on demand.


## v1.1.12

- skip optional dependencies that are not installed
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ If object is used, it contains following fields:
- `dir`: subdir to copy in this module. default the whole module, if not specified
- `link`: set true to use symlink instead of copying. default false.
- always false if `browserify` is set to true.
- `transpile`: add this object if you need to transpile module file, which contains following fields:
- `files`: a list of file to transpile.


## Publish
Expand Down
33 changes: 30 additions & 3 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, quit, cmds, slice$ = [].slice;
var colors, fs, path, os, fsExtra, browserify, yargs, child_process, glob, babel, quit, cmds, slice$ = [].slice;
colors = require('@plotdb/colors');
fs = require('fs');
path = require('path');
Expand All @@ -9,6 +9,7 @@ browserify = require('browserify');
yargs = require('yargs');
child_process = require('child_process');
glob = require('glob');
babel = require("@babel/core");
quit = function(){
console.error(Array.from(arguments).join(''));
return process.exit();
Expand Down Expand Up @@ -68,7 +69,7 @@ cmds['default'] = {
}
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;
var localModule, root, base, info, id, mainFile, ref$, i$, name, version, ret, that, desdir, maindir, p, srcdir, realSrcdir, ps, srcFile, desFile;
obj = typeof obj === 'string' ? {
name: obj
} : obj;
Expand Down Expand Up @@ -182,8 +183,34 @@ cmds['default'] = {
}
});
}
if (obj.transpile) {
ps = ((ref$ = obj.transpile).files || (ref$.files = [])).map(function(f){
return new Promise(function(res, rej){
var babelOpt, src, des;
babelOpt = {
presets: ['@babel/preset-env']
};
src = path.join(srcdir, f);
des = path.join(desdir, f);
return babel.transform(fs.readFileSync(src).toString(), babelOpt, function(err, result){
if (err) {
console.error("[ERROR] transpile ".red + f.brightYellow + " failed: ".red, err);
return rej(err);
}
fs.writeFileSync(des, result.code);
console.log(" -- " + "[JS/Transpile]".green + (" -> " + des));
return res();
});
});
});
p = Promise.all(ps)['catch'](function(e){
console.error("[ERROR] exception during transpilatio: ".red, e);
console.error("exit.".red);
return process.exit();
});
}
}
p = Promise.resolve().then(function(){
p = (p || Promise.resolve()).then(function(){
return console.log(" -- " + srcdir + " -> " + desdir + " ");
});
if (!obj.link && mainFile.js && !localModule) {
Expand Down
21 changes: 20 additions & 1 deletion lib/main.ls
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env node
require! <[@plotdb/colors fs path os fs-extra browserify yargs child_process glob]>
babel = require "@babel/core"

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

Expand Down Expand Up @@ -126,7 +127,25 @@ cmds.default =
{dereference: true, filter: -> !/.+[^.]\/node_modules|\/\.git/.exec(it)}
)
p = Promise.resolve!then -> console.log " -- #srcdir -> #desdir "
if obj.transpile =>
ps = obj.transpile.[]files.map (f) ->
(res, rej) <- new Promise _
babel-opt = {presets: <[@babel/preset-env]>}
src = path.join(srcdir, f)
des = path.join(desdir, f)
(err, result) <- babel.transform fs.read-file-sync(src).toString!, babel-opt, _
if err =>
console.error("[ERROR] transpile ".red + (f).brightYellow + " failed: ".red, err)
return rej err
fs.write-file-sync des, result.code
console.log " -- " + "[JS/Transpile]".green + " -> #des"
res!
p = Promise.all ps .catch (e) ->
console.error "[ERROR] exception during transpilatio: ".red, e
console.error "exit.".red
process.exit!

p = (p or Promise.resolve!).then -> console.log " -- #srcdir -> #desdir "
if !obj.link and main-file.js and !local-module =>
src-file = path.join(root, main-file.js)
des-file = path.join(desdir, "index.js")
Expand Down
Loading

0 comments on commit b1c8b92

Please sign in to comment.