Repository navigation
Expand file tree
/
Copy pathinit.js
More file actions
121 lines (106 loc) · 2.98 KB
/
Copy pathinit.js
File metadata and controls
121 lines (106 loc) · 2.98 KB
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
/*
* Copyright © 2018 LiquidPlayer
*
* Released under the MIT license. See LICENSE.md for terms.
*/
const fs = require('fs')
const usage = (() => {
/*
Description:
Initlializes a node module's package.json file for use with LiquidCore. Also
creates an 'example.js' service.
Usage:
npx liquidcore init [<options>]
Options:
--help Print this message
*/
}).toString().split(/\n/).slice(2, -2).join('\n')
const pkg = {
scripts: {
server: "liquidcore server",
bundler: "liquidcore bundle",
init: "liquidcore init",
"gradle-config": "liquidcore gradle",
"pod-config": "liquidcore pod",
postinstall: "liquidcore postinstall"
},
liquidcore: {
__nooverwrite: true,
entry: ["example.js"]
}
}
const merge = (config) => {
return new Promise((resolve,reject) => {
const write_package_json = (data) => {
fs.writeFile(config.dir + '/package.json', JSON.stringify(data,null,2), (err) => {
if (err) return reject(err)
console.log('package.json was updated.')
resolve(config)
})
}
fs.readFile(config.dir + '/package.json', (err,old)=>{
if (err) {
return reject(config.dir + '/package.json cannot be read. Make sure you run "npm init" first.')
}
var data =JSON.parse(old)
for (prop in pkg) {
if (typeof pkg[prop] === 'object') {
if (!data[prop] || typeof data.prop !== 'object') data[prop] = {}
for (p in pkg[prop]) {
if (p != '__nooverwrite' && (!pkg[prop].__nooverwrite || data[prop][p] === undefined))
data[prop][p] = pkg[prop][p]
}
} else {
data[prop] = pkg[prop]
}
}
write_package_json(data)
})
})
}
const cp_file = (config, file, name) => {
return new Promise((resolve,reject) => {
fs.stat(config.dir + '/' + name,(err) => {
if (!err) return resolve(config)
fs.readFile(__dirname + file, (err,data) => {
if (err) return reject(err)
fs.writeFile(config.dir + '/' + name, data, (err) => {
if (err) return reject(err)
console.log('Created file ' + name)
resolve(config)
})
})
})
})
}
const create_examplejs = (config) => cp_file(config, '/templates/helloworld.js','example.js')
const success = (config) => {
console.log('Successfully updated package.json for use with LiquidCore.')
return Promise.resolve(config)
};
async function prepare(override) {
var args = {}
Object.assign(args, override)
if (args.help) {
console.log(usage)
process.exit(0)
}
let config = {
dir: args.dir || '.'
}
fs.stat(config.dir,async (err,stats) => {
if (!err && !stats.isDirectory()) {
console.error('<project-dir> must be a directory containing package.json.')
process.exit(-3)
}
try {
await create_examplejs(config)
await merge(config)
await success(config)
} catch(err) {
console.error(err)
process.exit(-4)
}
})
}
module.exports = prepare