-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbin.js
executable file
·136 lines (129 loc) · 4.1 KB
/
bin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env node
const path = require('path');
const $ascjs = require('./index.js');
const escape = str => str.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
const ascjs = input => {
let output = $ascjs(input);
return noDefault ?
output
.replace(`${$ascjs.EXPORT}.default`, 'module.exports')
.replace(new RegExp(escape($ascjs.IMPORT), 'g'), ($0, index) => {
index += $0.length + 9;
const module = output.slice(index, output.indexOf(')', index + 1)).trim();
return /^['"]\./.test(module) ? '' : $0;
})
// .replace(/(^|[^)])\((require\(.+?\))\)/g, '$1$2')
:
output;
};
const argv = process.argv.slice(2);
const files = argv.filter(arg => /^[^-]/.test(arg));
const options = argv.filter(arg => /^-/.test(arg));
let noDefault = false;
const ignore = [];
options.forEach(arg => {
if (/^--no-default$/.test(arg))
noDefault = true;
else if (/^--ignore=/.test(arg))
ignore.push.apply(
ignore,
arg.slice(9).replace(/^('|")|('|")$/g, '').split(',')
.map(file => path.resolve(__dirname, file))
);
});
const source = files[0];
if (files.length < 1 && options.length) {
const info = require('./package.json');
console.log(`
\x1B[1mascjs\x1B[0m v${info.version}
${'-'.repeat(info.description.length)}
${info.description}
${'-'.repeat(info.description.length)}
# as executable
ascjs code
ascjs --ignore=a.js,b.js sourceFile
ascjs --no-default
ascjs sourceFile destFile
ascjs sourceFolder destFolder # dest is required
# as pipe
echo code | ascjs
cat sourceFile | ascjs
${'-'.repeat(info.description.length)}
${' '.repeat(info.description.length)
.slice(0, -(3 + info.author.length))}by ${info.author}
`);
} else if (files.length) {
const fs = require('fs');
const dest = files[1];
fs.stat(source, (err, stat) => {
if (err) {
process.stdout.write(ascjs(source));
} else if (stat.isFile()) {
fs.readFile(source, (err, source) => {
if (err) throw err;
if (dest) fs.writeFileSync(dest, ascjs(source));
else process.stdout.write(ascjs(source));
});
} else if (stat.isDirectory() && dest && fs.statSync(dest).isDirectory()) {
const cjsDest = path.resolve(process.cwd(), dest);
process.on('exit', () => {
const cjsPackage = path.join(cjsDest, 'package.json');
if (!fs.existsSync(cjsPackage))
fs.writeFileSync(cjsPackage, JSON.stringify({type: 'commonjs'}));
});
const mkdir = dir => {
try{ fs.mkdirSync(dir); }
catch(e){
if(e.errno === 34){
mkdir(path.dirname(dir));
mkdir(dir);
}
}
};
(function walkThrough(source, dest) {
fs.readdir(source, (err, files) => {
if (err) throw err;
files.forEach(file => {
if (ignore.includes(path.join(source, file))) return;
fs.stat(path.join(source, file), (err, stat) => {
if (err) throw err;
switch (true) {
case stat.isDirectory():
walkThrough(path.join(source, file), path.join(dest, file));
break;
case stat.isFile():
if (/\.(?:m\.?)?js$/.test(file)) {
fs.readFile(path.join(source, file), (err, content) => {
if (err) throw err;
mkdir(dest);
fs.writeFile(
path.join(dest, file),
ascjs(content),
(err) => {
if (err) throw err;
}
);
});
}
break;
}
});
});
});
}(
path.resolve(process.cwd(), source),
cjsDest
));
} else {
throw new Error('not sure what to do, try ascjs --help\n');
}
});
} else {
const chunks = [];
process.stdin.on('data', data => {
chunks.push(data);
});
process.stdin.on('end', () => {
process.stdout.write(ascjs(chunks.join('')));
});
}