@reactful/create 0.0.94 → 0.0.96
Sign up to get free protection for your applications and to get access to all the features.
- package/index.js +84 -80
- package/package.json +14 -8
package/index.js
CHANGED
@@ -1,12 +1,10 @@
|
|
1
1
|
#!/usr/bin/env node
|
2
2
|
|
3
|
-
|
3
|
+
import npmRegistryFetch from 'npm-registry-fetch'
|
4
4
|
import { execSync } from 'child_process'
|
5
|
-
import npmPackage from 'npm-package-arg'
|
6
|
-
import npmDownload from 'npm-download'
|
7
5
|
import inquirer from 'inquirer'
|
8
|
-
import fetch from 'node-fetch'
|
9
6
|
import path from 'path'
|
7
|
+
import * as tar from 'tar'
|
10
8
|
import fs from 'fs'
|
11
9
|
|
12
10
|
const GREEN = '\x1b[32m'
|
@@ -21,36 +19,43 @@ console.log(line)
|
|
21
19
|
|
22
20
|
const templates = ['empty', 'minimal', 'sampling']
|
23
21
|
|
24
|
-
const questions = [
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
{ type: 'confirm', name: 'install', message: 'Install?', prefix },
|
22
|
+
const questions = [{
|
23
|
+
type: 'list',
|
24
|
+
name: 'template',
|
25
|
+
message: 'Which template?',
|
26
|
+
choices: templates,
|
27
|
+
prefix
|
28
|
+
},
|
29
|
+
{ type: 'input', name: 'project', message: 'Project name?', prefix, default: 'Sample' },
|
30
|
+
{ type: 'confirm', name: 'vscode', message: 'VS Code IDE?', prefix },
|
31
|
+
{ type: 'confirm', name: 'install', message: 'Install?', prefix },
|
35
32
|
]
|
36
33
|
|
37
|
-
inquirer.prompt(questions).then(
|
38
|
-
|
39
|
-
|
34
|
+
inquirer.prompt(questions).then(prompting)
|
35
|
+
|
36
|
+
async function prompting(answers) {
|
37
|
+
if (!answers.project) throw 'Required project name!'
|
38
|
+
|
40
39
|
const destination = path.join(process.cwd(), answers.project)
|
41
|
-
|
42
|
-
|
43
|
-
await
|
44
|
-
|
40
|
+
const temporaryPath = path.join(destination, 'temp')
|
41
|
+
|
42
|
+
await createDirectory(destination)
|
43
|
+
|
44
|
+
console.log('- downloading package...')
|
45
|
+
await download('@reactful/create', temporaryPath)
|
46
|
+
|
47
|
+
console.log('- copying template...')
|
48
|
+
await copyTemplate(temporaryPath, `commons`, destination)
|
49
|
+
await copyTemplate(temporaryPath, `templates/${answers.template}`, destination)
|
50
|
+
await removeDirectory(temporaryPath)
|
51
|
+
|
45
52
|
console.log('- templating project...')
|
46
53
|
renamingJSON(destination, answers.project)
|
47
54
|
|
48
|
-
|
49
|
-
console.log('- installing dependencies...')
|
50
|
-
console.log('')
|
55
|
+
console.log('- installing dependencies...\n')
|
51
56
|
execSync(`cd ${answers.project}`)
|
52
57
|
execSync(`bun install`)
|
53
|
-
}
|
58
|
+
}
|
54
59
|
|
55
60
|
function renamingJSON(directory, projectName) {
|
56
61
|
const url = `${directory}/package.json`
|
@@ -62,70 +67,69 @@ function renamingJSON(directory, projectName) {
|
|
62
67
|
fs.writeFileSync(url, JSON.stringify(obj, null, 3))
|
63
68
|
}
|
64
69
|
|
65
|
-
|
66
|
-
downloadNPM('@reactful/create', subfolder, destination)
|
67
|
-
|
68
|
-
async function downloadNPM(packageName, subdirectory, destination) {
|
70
|
+
async function copyTemplate(temporaryPath, subdirectory, destination) {
|
69
71
|
try {
|
70
|
-
|
71
|
-
|
72
|
-
const from = path.join(content, subdirectory, destination)
|
73
|
-
const notFound = fs.existsSync(destination) == false
|
72
|
+
const folder = path.join(temporaryPath, subdirectory)
|
73
|
+
await copyFolder(folder, destination)
|
74
74
|
|
75
|
-
if (notFound) fs.mkdirSync(destination, { recursive: true })
|
76
|
-
|
77
|
-
fs.renameSync(from, destination)
|
78
75
|
} catch (error) {
|
79
|
-
|
76
|
+
console.error('copyTemplate')
|
77
|
+
throw error
|
80
78
|
}
|
81
79
|
}
|
82
80
|
|
83
|
-
async function
|
84
|
-
|
81
|
+
async function download(packageName, directory) {
|
82
|
+
try {
|
83
|
+
await createDirectory(directory)
|
85
84
|
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
85
|
+
const metadata = await npmRegistryFetch.json(packageName)
|
86
|
+
const latestVersion = metadata['dist-tags'].latest
|
87
|
+
const tarballURL = metadata.versions[latestVersion].dist.tarball
|
88
|
+
const filename = tarballURL.substring(tarballURL.lastIndexOf('/') + 1)
|
89
|
+
const tempFilePath = path.join(directory, filename)
|
90
90
|
|
91
|
-
|
92
|
-
|
93
|
-
logging(response, contents)
|
94
|
-
throw 'failed to download scafold from github...'
|
95
|
-
}
|
91
|
+
if (!fs.existsSync(directory))
|
92
|
+
fs.mkdirSync(directory, { recursive: true })
|
96
93
|
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
const
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
if (notFound) fs.mkdirSync(baseFile, { recursive: true })
|
114
|
-
|
115
|
-
fs.writeFileSync(filePath, fileText)
|
116
|
-
}
|
117
|
-
catch(ex) {
|
118
|
-
console.log('MOMMENT: file...')
|
119
|
-
logging(response, contents, item.download_url)
|
120
|
-
throw ex
|
121
|
-
}
|
94
|
+
const response = await npmRegistryFetch(tarballURL)
|
95
|
+
|
96
|
+
await new Promise((resolve, reject) => {
|
97
|
+
const fileStream = fs.createWriteStream(tempFilePath)
|
98
|
+
response.body.pipe(fileStream)
|
99
|
+
response.body.on('error', reject)
|
100
|
+
fileStream.on('finish', resolve)
|
101
|
+
})
|
102
|
+
|
103
|
+
await tar.x({ file: tempFilePath, cwd: directory, strip: 1 })
|
104
|
+
|
105
|
+
fs.unlinkSync(tempFilePath)
|
106
|
+
} catch (error) {
|
107
|
+
console.error('downloadPackage')
|
108
|
+
throw error
|
122
109
|
}
|
123
110
|
}
|
124
111
|
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
112
|
+
|
113
|
+
function createDirectory(directory) {
|
114
|
+
removeDirectory(directory)
|
115
|
+
fs.mkdirSync(directory)
|
116
|
+
}
|
117
|
+
|
118
|
+
function removeDirectory(directory) {
|
119
|
+
if (!fs.existsSync(directory)) return
|
120
|
+
else fs.rmSync(directory, { recursive: true })
|
121
|
+
}
|
122
|
+
|
123
|
+
function copyFolder(source, target) {
|
124
|
+
if (!fs.existsSync(target)) fs.mkdirSync(target)
|
125
|
+
|
126
|
+
fs.readdirSync(source).forEach(function (file) {
|
127
|
+
const currentPath = path.join(source, file)
|
128
|
+
const targetPath = path.join(target, file)
|
129
|
+
|
130
|
+
if (fs.lstatSync(currentPath).isDirectory())
|
131
|
+
copyFolder(currentPath, targetPath)
|
132
|
+
|
133
|
+
else fs.copyFileSync(currentPath, targetPath)
|
134
|
+
})
|
131
135
|
}
|
package/package.json
CHANGED
@@ -1,20 +1,26 @@
|
|
1
1
|
{
|
2
2
|
"name": "@reactful/create",
|
3
|
-
"version": "0.0.
|
3
|
+
"version": "0.0.96",
|
4
4
|
"main": "index.js",
|
5
5
|
"type": "module",
|
6
6
|
"description": "reactful scafold tool",
|
7
7
|
"author": "jonathan de sena ribeiro <jsenaribeiro@gmail.com>",
|
8
|
-
"files": ["index.js", "package.json", "templates", "commons"],
|
9
8
|
"bin": { "@reactful/create": "index.js" },
|
10
|
-
"
|
9
|
+
"files": [
|
10
|
+
"index.js",
|
11
|
+
"package.json",
|
12
|
+
"templates",
|
13
|
+
"commons"
|
14
|
+
],
|
15
|
+
"scripts": {
|
16
|
+
"start": "bun run index.js",
|
17
|
+
"deploy": "npm publish --access public"
|
18
|
+
},
|
11
19
|
"license": "MIT",
|
12
20
|
"dependencies": {
|
13
21
|
"bun-types": "latest",
|
14
|
-
"fs": "^0.0.1-security",
|
15
22
|
"inquirer": "^9.2.13",
|
16
|
-
"
|
17
|
-
"
|
18
|
-
"npm-package-arg": "^11.0.1"
|
23
|
+
"npm-registry-fetch": "^16.1.0",
|
24
|
+
"tar": "^6.2.0"
|
19
25
|
}
|
20
|
-
}
|
26
|
+
}
|