@reactful/create 0.0.95 → 0.0.97
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -2
- package/index.js +84 -79
- package/package.json +14 -8
package/README.md
CHANGED
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'
|
@@ -22,33 +20,43 @@ console.log(line)
|
|
22
20
|
const templates = ['empty', 'minimal', 'sampling']
|
23
21
|
|
24
22
|
const questions = [{
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
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 },
|
34
32
|
]
|
35
33
|
|
36
|
-
inquirer.prompt(questions).then(
|
37
|
-
|
38
|
-
|
34
|
+
inquirer.prompt(questions).then(prompting)
|
35
|
+
|
36
|
+
async function prompting(answers) {
|
37
|
+
if (!answers.project) throw 'Required project name!'
|
38
|
+
|
39
39
|
const destination = path.join(process.cwd(), answers.project)
|
40
|
-
|
41
|
-
|
42
|
-
await
|
43
|
-
|
40
|
+
const temporaryPath = path.join(destination, 'temp')
|
41
|
+
|
42
|
+
await createDirectory(destination)
|
43
|
+
|
44
|
+
console.log('- downloading NPM...')
|
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
|
+
|
44
52
|
console.log('- templating project...')
|
45
53
|
renamingJSON(destination, answers.project)
|
46
|
-
|
47
|
-
console.log('- installing dependencies
|
48
|
-
console.log('')
|
54
|
+
|
55
|
+
console.log('- installing dependencies...\n')
|
49
56
|
execSync(`cd ${answers.project}`)
|
57
|
+
execSync(`ls`)
|
50
58
|
execSync(`bun install`)
|
51
|
-
}
|
59
|
+
}
|
52
60
|
|
53
61
|
function renamingJSON(directory, projectName) {
|
54
62
|
const url = `${directory}/package.json`
|
@@ -60,72 +68,69 @@ function renamingJSON(directory, projectName) {
|
|
60
68
|
fs.writeFileSync(url, JSON.stringify(obj, null, 3))
|
61
69
|
}
|
62
70
|
|
63
|
-
|
64
|
-
|
71
|
+
async function copyTemplate(temporaryPath, subdirectory, destination) {
|
72
|
+
try {
|
73
|
+
const folder = path.join(temporaryPath, subdirectory)
|
74
|
+
await copyFolder(folder, destination)
|
65
75
|
|
66
|
-
|
67
|
-
|
76
|
+
} catch (error) {
|
77
|
+
console.error('copyTemplate')
|
78
|
+
throw error
|
79
|
+
}
|
80
|
+
}
|
68
81
|
|
82
|
+
async function download(packageName, directory) {
|
69
83
|
try {
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
84
|
+
await createDirectory(directory)
|
85
|
+
|
86
|
+
const metadata = await npmRegistryFetch.json(packageName)
|
87
|
+
const latestVersion = metadata['dist-tags'].latest
|
88
|
+
const tarballURL = metadata.versions[latestVersion].dist.tarball
|
89
|
+
const filename = tarballURL.substring(tarballURL.lastIndexOf('/') + 1)
|
90
|
+
const tempFilePath = path.join(directory, filename)
|
91
|
+
|
92
|
+
if (!fs.existsSync(directory))
|
93
|
+
fs.mkdirSync(directory, { recursive: true })
|
74
94
|
|
75
|
-
|
95
|
+
const response = await npmRegistryFetch(tarballURL)
|
76
96
|
|
77
|
-
|
97
|
+
await new Promise((resolve, reject) => {
|
98
|
+
const fileStream = fs.createWriteStream(tempFilePath)
|
99
|
+
response.body.pipe(fileStream)
|
100
|
+
response.body.on('error', reject)
|
101
|
+
fileStream.on('finish', resolve)
|
102
|
+
})
|
103
|
+
|
104
|
+
await tar.x({ file: tempFilePath, cwd: directory, strip: 1 })
|
105
|
+
|
106
|
+
fs.unlinkSync(tempFilePath)
|
78
107
|
} catch (error) {
|
79
|
-
|
108
|
+
console.error('downloadPackage')
|
109
|
+
throw error
|
80
110
|
}
|
81
111
|
}
|
82
112
|
|
83
|
-
async function downloadGitHub(user, repository, subdir, destination) {
|
84
|
-
console.log(`- downloading ${subdir}...`)
|
85
|
-
|
86
|
-
const prefix = `https://api.github.com/repos/${user}/${repository}`
|
87
|
-
const address = `${prefix}/contents/installation/${subdir}`
|
88
|
-
const response = await fetch(address)
|
89
|
-
const contents = await response.json()
|
90
113
|
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
}
|
114
|
+
function createDirectory(directory) {
|
115
|
+
removeDirectory(directory)
|
116
|
+
fs.mkdirSync(directory)
|
117
|
+
}
|
96
118
|
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
const goto = `${destination}/${item.name}`
|
101
|
-
await download(from, goto); continue
|
102
|
-
}
|
103
|
-
|
104
|
-
if (item.type != 'file') continue
|
105
|
-
|
106
|
-
try {
|
107
|
-
const filePath = path.join(destination, item.name)
|
108
|
-
const response = await fetch(item.download_url)
|
109
|
-
const fileText = await response.text()
|
110
|
-
const baseFile = path.dirname(filePath)
|
111
|
-
const notFound = fs.existsSync(baseFile) == false
|
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
|
-
}
|
122
|
-
}
|
119
|
+
function removeDirectory(directory) {
|
120
|
+
if (!fs.existsSync(directory)) return
|
121
|
+
else fs.rmSync(directory, { recursive: true })
|
123
122
|
}
|
124
123
|
|
125
|
-
function
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
124
|
+
function copyFolder(source, target) {
|
125
|
+
if (!fs.existsSync(target)) fs.mkdirSync(target)
|
126
|
+
|
127
|
+
fs.readdirSync(source).forEach(function (file) {
|
128
|
+
const currentPath = path.join(source, file)
|
129
|
+
const targetPath = path.join(target, file)
|
130
|
+
|
131
|
+
if (fs.lstatSync(currentPath).isDirectory())
|
132
|
+
copyFolder(currentPath, targetPath)
|
133
|
+
|
134
|
+
else fs.copyFileSync(currentPath, targetPath)
|
135
|
+
})
|
131
136
|
}
|
package/package.json
CHANGED
@@ -1,20 +1,26 @@
|
|
1
1
|
{
|
2
2
|
"name": "@reactful/create",
|
3
|
-
"version": "0.0.
|
3
|
+
"version": "0.0.97",
|
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
|
+
}
|