-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
126 lines (109 loc) · 2.99 KB
/
index.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
#!/usr/bin/env node
const init = require('init-package-json')
const fs = require('fs')
const readline = require('readline')
const path = require('path')
const initFile = path.resolve(process.env.HOME, '.npm-init')
const cwd = process.cwd()
let package
let rl
console.log(`This tool will walk you through creating a plugin for Citation.js.
You can leave defaults for 'main' and 'test', those will be filled
in later.
`)
init(cwd, initFile, (er, data) => {
if (er) {
console.log()
process.exit(0)
}
package = data
package.main = package.main === 'index.js' ? 'lib/index.js' : package.main
Object.assign(package, {
engines: {
node: '>=14'
},
files: [
'/lib'
],
standard: {
parser: '@babel/eslint-parser'
},
scripts: {
test: 'mocha -r @babel/register test/suite.js',
babel: 'babel src -d lib --copy-files',
lint: 'standard "src/**/*.js" "test/**/*.js"',
changelog: 'conventional-changelog -p angular -i CHANGELOG.md -s -r 0 && git add CHANGELOG.md',
coverage: 'NODE_ENV=coverage nyc npm test',
report: 'nyc report --reporter=lcov > coverage.lcov',
preversion: 'npm run lint && npm run test',
version: 'npm run changelog',
postversion: 'npm run babel'
},
dependencies: {
'@citation-js/date': '^0.5.0',
'@citation-js/name': '^0.4.2',
},
devDependencies: {
'@babel/cli': '^7.12.10',
'@babel/core': '^7.12.10',
'@babel/eslint-parser': '^7.14.3',
'@babel/preset-env': '^7.12.10',
'@babel/register': '^7.12.10',
'babel-plugin-istanbul': '^5.1.0',
'@citation-js/core': '^0.5.1',
'conventional-changelog-cli': '^2.1.0',
mocha: '^8.4.0',
nyc: '^15.1.0',
standard: '^16.0.3'
},
peerDependencies: {
'@citation-js/core': '^0.5.1'
}
})
fs.writeFileSync('package.json', JSON.stringify(package, null, 2))
fs.writeFileSync('.gitignore', `/lib
/node_modules
/package-lock.json
/.nyc_output`)
rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
rl.question('What should the plugin scope name (csl, doi, bibtex) be? ', copyTemplate)
})
function copyTemplate (scope) {
const input = {
$PACKAGE: package.name,
$SCOPE: scope
}
rl.close()
const template = path.join(__dirname, 'template')
function copy (dir) {
const source = path.join(template, dir)
const files = fs.readdirSync(source)
try {
fs.mkdirSync(dir)
} catch (e) {
if (e.code === 'EEXIST') {
// expected for dir === '.'
} else {
throw e
}
}
for (let file of files) {
const target = path.join(dir, file)
try {
let text = fs.readFileSync(path.join(source, file), 'utf8')
text = text.replace(/\$\w+/g, (variable) => input[variable])
fs.writeFileSync(target, text)
} catch (e) {
if (e.code === 'EISDIR') {
copy(target)
} else {
throw e
}
}
}
}
copy('.')
}