@makano/rew 1.1.0 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- package/lib/rew/cli/cli.js +19 -1
- package/lib/rew/cli/utils.js +71 -3
- package/lib/rew/functions/future.js +2 -1
- package/lib/rew/functions/import.js +24 -1
- package/package.json +1 -1
package/lib/rew/cli/cli.js
CHANGED
@@ -3,9 +3,11 @@
|
|
3
3
|
const yargs = require('yargs/yargs');
|
4
4
|
const path = require('path');
|
5
5
|
const { hideBin } = require('yargs/helpers');
|
6
|
-
const { fork } = require('child_process');
|
6
|
+
const { fork, exec } = require('child_process');
|
7
7
|
const { watch } = require('chokidar');
|
8
8
|
const utils = require('./utils');
|
9
|
+
const { existsSync } = require('fs');
|
10
|
+
const { log } = require('./log');
|
9
11
|
|
10
12
|
yargs(hideBin(process.argv))
|
11
13
|
.command(
|
@@ -25,6 +27,10 @@ yargs(hideBin(process.argv))
|
|
25
27
|
},
|
26
28
|
(argv) => {
|
27
29
|
const filePath = path.resolve(process.cwd(), argv.file);
|
30
|
+
if(!existsSync(filePath)){
|
31
|
+
log('File not found:', argv.file, ':end');
|
32
|
+
return;
|
33
|
+
}
|
28
34
|
const watching = [];
|
29
35
|
const watchIt = (file) => {
|
30
36
|
if(watching.includes(file)) return;
|
@@ -104,6 +110,18 @@ yargs(hideBin(process.argv))
|
|
104
110
|
utils.runApp(argv.path);
|
105
111
|
}
|
106
112
|
)
|
113
|
+
.command('install <path>', 'Install an app', (yargs) => {
|
114
|
+
yargs
|
115
|
+
.positional('path', {
|
116
|
+
describe: 'Path of the app to install',
|
117
|
+
type: 'string',
|
118
|
+
});
|
119
|
+
},
|
120
|
+
async (argv) => {
|
121
|
+
if(argv.path.startsWith('github:')) utils.installApp(await utils.cloneGit(argv.path), true, true);
|
122
|
+
else utils.installApp(argv.path);
|
123
|
+
}
|
124
|
+
)
|
107
125
|
.command('build <file>', 'Build the specified file', (yargs) => {
|
108
126
|
yargs
|
109
127
|
.positional('file', {
|
package/lib/rew/cli/utils.js
CHANGED
@@ -4,8 +4,9 @@ const conf = require('../pkgs/conf');
|
|
4
4
|
const jsYaml = require('js-yaml');
|
5
5
|
const readline = require('readline');
|
6
6
|
const { log, logget } = require('./log');
|
7
|
-
const { execSync } = require('child_process');
|
7
|
+
const { execSync, exec } = require('child_process');
|
8
8
|
const { run } = require('../main');
|
9
|
+
const { generateRandomID } = require('../functions/id');
|
9
10
|
|
10
11
|
const npm_package_name = '@makano/rew';
|
11
12
|
|
@@ -68,6 +69,7 @@ module.exports = {
|
|
68
69
|
fs.writeFileSync(confPath, jsYaml.dump({ package: project.package, entry: 'main.coffee' }));
|
69
70
|
fs.writeFileSync(entryFile, `print("Hello World!")`);
|
70
71
|
if(project.git) {
|
72
|
+
fs.writeFileSync(path.join(projectPath, '.gitignore'), `node_modules/\npackage-lock.json`);
|
71
73
|
execSync('cd '+projectPath+' && git init .');
|
72
74
|
}
|
73
75
|
log('Installing '+npm_package_name);
|
@@ -104,7 +106,14 @@ module.exports = {
|
|
104
106
|
const runAppRoot = (root, confPath) => {
|
105
107
|
const c = jsYaml.load(fs.readFileSync(confPath, { encoding: 'utf-8' }));
|
106
108
|
if(c.entry){
|
107
|
-
|
109
|
+
const r = path.resolve(root, c.entry);
|
110
|
+
const mod_path = path.resolve(root, 'node_modules/@makano/rew');
|
111
|
+
const mod_path_lib = path.join(mod_path, 'lib/rew/cli');
|
112
|
+
if(fs.existsSync(mod_path) && __dirname !== mod_path_lib){
|
113
|
+
const mod_path_utilsjs = path.join(mod_path_lib, '../main.js');
|
114
|
+
require(mod_path_utilsjs)
|
115
|
+
.run(r);
|
116
|
+
} else run(r);
|
108
117
|
}
|
109
118
|
}
|
110
119
|
|
@@ -112,11 +121,70 @@ module.exports = {
|
|
112
121
|
runAppRoot(apppath, appConfpath);
|
113
122
|
} else {
|
114
123
|
const con = conf({});
|
115
|
-
const apppath = path.resolve(con.CONFIG_PATH, pathOrPackage, '
|
124
|
+
const apppath = path.resolve(con.CONFIG_PATH, pathOrPackage, 'app');
|
116
125
|
const appConfpath = path.join(apppath, 'app.yaml');
|
117
126
|
if(fs.existsSync(apppath) && fs.existsSync(appConfpath)){
|
118
127
|
runAppRoot(apppath, appConfpath);
|
119
128
|
}
|
120
129
|
}
|
130
|
+
},
|
131
|
+
installApp(pathname, rmidir, rmidiri){
|
132
|
+
if(!pathname){
|
133
|
+
return;
|
134
|
+
}
|
135
|
+
const apppath = path.resolve(process.cwd(), pathname);
|
136
|
+
const appConfpath = path.join(apppath, 'app.yaml');
|
137
|
+
const appPackagepath = path.join(apppath, 'package.json');
|
138
|
+
if(fs.existsSync(apppath) && fs.existsSync(appConfpath)){
|
139
|
+
const c = jsYaml.load(fs.readFileSync(appConfpath, { encoding: 'utf-8' }));
|
140
|
+
const p = JSON.parse(fs.readFileSync(appPackagepath, { encoding: 'utf-8' }));
|
141
|
+
const pname = c.package;
|
142
|
+
const installPath = path.join(conf({}).create(pname).root, 'app');
|
143
|
+
const rl = readline.createInterface({
|
144
|
+
input: process.stdin,
|
145
|
+
output: process.stdout
|
146
|
+
});
|
147
|
+
log('Installing '+pname);
|
148
|
+
log("Package: "+p.name+'@'+p.version);
|
149
|
+
if(p.description){
|
150
|
+
log("Description: "+p.description);
|
151
|
+
}
|
152
|
+
rl.question(logget('Install '+pname+'? (y/N)'), (f) => {
|
153
|
+
if(f.toLowerCase() == 'y'){
|
154
|
+
if(fs.existsSync(installPath)){
|
155
|
+
execSync(`rm -r ${installPath}`);
|
156
|
+
}
|
157
|
+
execSync(`cp -r ${apppath} ${installPath}`);
|
158
|
+
if(rmidir){
|
159
|
+
execSync(`rm -r ${apppath}`);
|
160
|
+
}
|
161
|
+
log('Installed '+pname, ':end');
|
162
|
+
rl.close();
|
163
|
+
} else {
|
164
|
+
if(rmidiri){
|
165
|
+
execSync(`rm -r ${apppath}`);
|
166
|
+
}
|
167
|
+
log('Canceled install', ':end');
|
168
|
+
rl.close();
|
169
|
+
}
|
170
|
+
});
|
171
|
+
} else {
|
172
|
+
log('Path is not a rew app', ':end');
|
173
|
+
}
|
174
|
+
},
|
175
|
+
async cloneGit(gitpath){
|
176
|
+
const p = gitpath.split('github:')[1];
|
177
|
+
const url = `https://github.com/${p}`;
|
178
|
+
const apiurl = `https://api.github.com/repos/${p}`;
|
179
|
+
return await fetch(apiurl)
|
180
|
+
.then((r) => {
|
181
|
+
if(r.status !== 200) return log('Repo not found', ':end');
|
182
|
+
const tempPath = '/tmp/rew-git-clone-'+p.replace(/\//g, '_')+'-'+generateRandomID();
|
183
|
+
execSync(`git clone ${url} ${tempPath}`);
|
184
|
+
console.log('Installing deps...');
|
185
|
+
execSync(`cd ${tempPath} && npm i`);
|
186
|
+
return tempPath;
|
187
|
+
})
|
188
|
+
.catch(r => null);
|
121
189
|
}
|
122
190
|
}
|
@@ -14,9 +14,10 @@ module.exports = function future(callback, timeout = 0, defData = null) {
|
|
14
14
|
});
|
15
15
|
return {
|
16
16
|
pipe: (callback) => promise.then(callback),
|
17
|
-
|
17
|
+
last: (callback) => promise.finally(callback),
|
18
18
|
catch: (callback) => promise.catch(callback),
|
19
19
|
resolve: (data) => listener.emit("resolve", data),
|
20
20
|
reject: (data) => listener.emit("reject", data),
|
21
|
+
wait: async () => await promise
|
21
22
|
};
|
22
23
|
};
|
@@ -2,6 +2,23 @@ const path = require("path");
|
|
2
2
|
const { getFile, file } = require("../modules/fs");
|
3
3
|
const { importYaml } = require("../modules/yaml");
|
4
4
|
const { findPackage, getPackage } = require("../pkgs/pkgs");
|
5
|
+
const { existsSync, readFileSync } = require("fs");
|
6
|
+
const conf = require("../pkgs/conf");
|
7
|
+
const jsYaml = require("js-yaml");
|
8
|
+
|
9
|
+
const lookUpInOtherApps = (fullPath) => {
|
10
|
+
const con = conf({});
|
11
|
+
const name = fullPath.indexOf('/') ? fullPath.split('/')[0] : fullPath;
|
12
|
+
let dpath = fullPath.indexOf('/') ? fullPath.split('/')[1] : '';
|
13
|
+
let ppath = path.join(con.CONFIG_PATH, name, 'app');
|
14
|
+
if(!existsSync(ppath)) return null;
|
15
|
+
if(!dpath){
|
16
|
+
dpath = jsYaml.load(readFileSync(path.join(ppath, 'app.yaml'), 'utf-8')).entry;
|
17
|
+
}
|
18
|
+
ppath = path.join(ppath, dpath);
|
19
|
+
if(existsSync(ppath)) return ppath;
|
20
|
+
else return null;
|
21
|
+
}
|
5
22
|
|
6
23
|
module.exports.imp = function (runPath, context) {
|
7
24
|
return function (filename, options = {}) {
|
@@ -9,10 +26,16 @@ module.exports.imp = function (runPath, context) {
|
|
9
26
|
let exports,
|
10
27
|
ispkg = findPackage(filename);
|
11
28
|
|
12
|
-
|
29
|
+
let filepath = path.resolve(path.dirname(context.module.filepath), filename);
|
13
30
|
|
14
31
|
// console.log(typeof runPath);
|
15
32
|
|
33
|
+
if(!ispkg && !existsSync(filepath)){
|
34
|
+
const otherPath = lookUpInOtherApps(filename);
|
35
|
+
if(!otherPath) throw new Error('Module "'+filename+'" not found');
|
36
|
+
else filepath = otherPath;
|
37
|
+
}
|
38
|
+
|
16
39
|
if (ispkg) {
|
17
40
|
exports = getPackage(filename)(context);
|
18
41
|
} else if (type == "coffee") {
|