UNPKG

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