-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-sequence.js
executable file
·115 lines (88 loc) · 3.08 KB
/
create-sequence.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
#!/usr/bin/env node
/* eslint-disable no-console */
"use strict"
const fse = require("fs-extra")
const init = require("init-package-json")
const fs = require("fs/promises")
const path = require("path")
const templateName = process.argv[2] || "js-transformer"
const exists = async (path) => {
return fs.access(path)
.then(
() => true,
() => false
)
}
async function initFiles() {
const { INIT_CWD, PWD } = process.env;
const workingDirectory = path.resolve(INIT_CWD || PWD || undefined);
const packageJsonPath = path.resolve(workingDirectory, "package.json")
if (await exists(packageJsonPath)) {
throw new Error(`Error: package.json already exists in "${packageJsonPath}"`)
}
const templatesPath = path.join(__dirname, "templates", path.resolve("/", templateName))
if (!(await exists(templatesPath))) {
throw new Error(`Error: Template ${templateName} couldn't be found`)
}
const workingDirectoryFiles = await fs.readdir(workingDirectory)
const templatesDirectoryFiles = await fs.readdir(templatesPath)
for (const file of templatesDirectoryFiles) {
if (workingDirectoryFiles.includes(file)) {
throw new Error(`Error: ${file} already exists in current location`)
}
}
const templatePackageJSON = await (async () => {
try {
return JSON.parse(await fs.readFile(path.resolve(templatesPath, "package.json"), "utf8"))
} catch (_e) {
throw new Error("Error while reading template package.json")
}
})()
return {
packageJsonPath,
templatesPath,
workingDirectory,
templatePackageJSON
}
}
async function initPackage(data) {
const { templatePackageJSON, packageJsonPath, workingDirectory } = data
console.log("")
{
const { name, version, main, engines } = templatePackageJSON
await fs.writeFile(packageJsonPath, JSON.stringify({ name, version, main, engines }, null, 2))
}
const userPackageJSON = await init(workingDirectory, path.resolve(__dirname, "default-input.js"), templatePackageJSON)
console.log("")
if (!userPackageJSON) {
throw new Error("Sequence template initialization canceled")
}
const packageJson = { ...templatePackageJSON, ...userPackageJSON }
if (userPackageJSON.scripts.test === 'echo "Error: no test specified" && exit 1') {
delete userPackageJSON.scripts.test
}
packageJson.scripts = { ...templatePackageJSON.scripts, ...userPackageJSON.scripts }
data.pkgJSON = packageJson
return data
}
async function copyFiles(data) {
const { templatesPath, workingDirectory } = data
await fse.copy(templatesPath, workingDirectory, { overwrite: false, errorOnExist: true, filter: (src) => !src.endsWith("package.json") })
return data
}
async function copyPackage(data) {
const { packageJsonPath, pkgJSON } = data
await fs.writeFile(packageJsonPath, JSON.stringify(pkgJSON, null, 2))
console.log("Sequence template succesfully created")
return data
}
function errorHandler(error) {
console.error(error.message ? error.message : error)
}
Promise
.resolve()
.then(initFiles)
.then(initPackage)
.then(copyFiles)
.then(copyPackage)
.catch(errorHandler)