-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
126 lines (126 loc) · 4.54 KB
/
index.js
File metadata and controls
126 lines (126 loc) · 4.54 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
122
123
124
125
126
const npm = require('npm'),
fs = require('fs').promises,
parser = require('comment-parser'),
path = require('path'),
chokidar = require('chokidar'),
resolve = require('path').resolve
const debug = (program, m) => {
if (!program.debug) return;
console.log(JSON.stringify(m))
}
// Require local dependencies
const script = require('./script')
/**
* Initialize!
*
* @param {Object} program Commander's original object
*/
const init = async (program) => {
debug(program, `Init. Program file: ${program.file}`)
program.file = path.resolve(program.file)
// Only execute if the target file exists
try {
await fs.access(program.file)
} catch (error) {
console.error(program.file)
throw new Error('file-not-found')
}
debug(program, 'File found')
// Initialize path-related variables
const targetPath = path.dirname(resolve(program.file))
debug(program, `Target path: ${targetPath}`)
const run = async () => {
const contents = await fs.readFile(program.file, 'utf-8')
const packageExists = await fs.access(`${targetPath}/package.json`).then(() => true).catch(() => false)
debug(program, `Package file exists: ${packageExists}`)
// If there's an existing package.json file for the specified file,
// skip the dependencies installation and execute the script.
if (packageExists) {
debug(program, 'A package.json file already exists.')
debug(program, 'Skipping dependencies installation.')
return script.execute(program, targetPath)
}
debug(program, `Parse comments...`)
// Parse the comments for the targeted file
const comments = parser(contents)
// Hold a list of dependencies, as in:
// [ "package@version" ]
let dependencies = []
// Find the comment that contains the dependencies
const dependenciesComments = comments.filter(comment => {
return (comment.tags || []).find(t => t.tag === 'dependency')
})
// If there's no dependencies comment, execute the script normally.
if (!dependenciesComments) {
debug(program, 'No dependencies comment found. Executing script.')
return script.execute(program, targetPath)
}
// For all the js-doc tags found in the dependencies comment, grab and parse
// only the ones that contain actual requirements.
dependenciesComments.map(dc => {
dc.tags.map(tag => {
if (tag.tag !== 'dependency') return;
debug(program, `Adding dependency ${tag.name}@${tag.description}`)
dependencies.push(`${tag.name}@${tag.description}`)
})
})
// If there are no dependencies found in the comment, execute the script
// normally
if (!dependencies.length) {
debug(program, 'No dependencies comment found. Executing script.')
return script.execute(program, targetPath)
}
if (program.dryrun) {
// Remove the node_modules folder to ensure a clean execution
debug(program, 'Performing dry run')
await fs.rm(`${targetPath}/node_modules`, { recursive: true, force: true })
await fs.unlink(`${targetPath}/package-lock.json`).catch(() => {})
}
debug(program, 'Init main promise')
return new Promise((resolve, reject) => {
debug(program, 'Created main promise')
// Init npm package
npm.load({
prefix: targetPath
}, () => {
debug(program, 'npm loaded')
// Install the dependencies
script
.executeNpm(['install', '--silent', '--no-audit', '--no-progress', '--no-save'].concat(dependencies), targetPath)
.then(() => {
debug(program, 'Dependencies installed. Running now')
return script.execute(program, targetPath)
.then(result => {
if (program.watch) {
console.log('Clean exit - waiting for file changes to restart')
}
resolve(result)
})
.catch(ex => {
debug(program, 'Script execution error')
if (program.watch) {
console.log('Error - waiting for file changes to restart')
}
})
})
.catch(ex => {
debug(program, 'NPM error')
debug(program, ex)
})
})
})
}
if (program.watch) {
debug(program, 'Watching for changes')
chokidar.watch(program.file, {
persistent: true
})
.on('change', path => {
console.log('Changes detected - restarting')
run()
})
}
debug(program, 'nodemon-like integration disabled')
return run()
}
module.exports.init = init