@reactful/create 1.2.19 → 1.2.20

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. package/index.js +147 -147
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -1,147 +1,147 @@
1
- #!/usr/bin/env node
2
-
3
- import npmRegistryFetch from 'npm-registry-fetch'
4
- import { spawn , execSync } from 'child_process'
5
- import inquirer from 'inquirer'
6
- import path from 'path'
7
- import * as tar from 'tar'
8
- import fs from 'fs'
9
-
10
- const CIANO = '\x1b[36m'
11
- const GREEN = '\x1b[32m'
12
- const RESET = '\x1b[0m'
13
- const prefix = `${GREEN}+${RESET}`
14
- const char = '─'
15
- const line = char.repeat(51)
16
- const decor = '-'.repeat(18)
17
-
18
- const packageJson = fs.readFileSync('package.json', 'utf-8');
19
- const { version } = JSON.parse(packageJson);
20
-
21
- console.log('___________________________________________________')
22
- console.log('')
23
- console.log(` ${GREEN}REACTful${RESET} create `)
24
- console.log(` ${CIANO}current version ${version}${RESET} `)
25
- //console.log(`${decor}( ${GREEN}reactful${RESET}.js )${decor}`)
26
- console.log('___________________________________________________')
27
- // console.log(line)
28
- console.log('')
29
-
30
- const templates = ['empty', 'minimal', 'sampling']
31
- const questions = [{
32
- type: 'list',
33
- name: 'template',
34
- message: 'Which template?',
35
- choices: templates,
36
- prefix
37
- },
38
- { type: 'input', name: 'project', message: 'Project name?', prefix, default: 'Sample' },
39
- { type: 'confirm', name: 'vscode', message: 'VS Code IDE?', prefix },
40
- { type: 'confirm', name: 'install', message: 'Install?', prefix },
41
- ]
42
-
43
- inquirer.prompt(questions).then(prompting)
44
-
45
- async function prompting(answers) {
46
- console.log('- preparing...')
47
-
48
- if (!answers.project) throw 'Required project name!'
49
-
50
- const destination = path.join(process.cwd(), answers.project)
51
- const downloadPath = path.join(destination, 'temp')
52
-
53
- await createDirectory(destination)
54
-
55
- console.log('- loading NPM...')
56
- await download('@reactful/create', downloadPath)
57
-
58
- console.log('- creating env...')
59
- const env = `PORT=333\nMINIFY=FALSE`
60
- fs.writeFileSync(`${destination}/.env`, env)
61
-
62
- console.log('- copying template...')
63
- await copyPath(`${downloadPath}/commons`, destination)
64
- await copyPath(`${downloadPath}/templates/${answers.template}`, destination)
65
-
66
- console.log('- renaming project...')
67
- renamingJSON(destination, answers.project)
68
-
69
- console.log('- installing dependencies...\n')
70
- process.chdir(answers.project)
71
- execSync(`bun install`)
72
-
73
- if (answers.vscode) {
74
- spawn("code", ['.'])
75
-
76
- const pathVSCode = `${destination}/.vscode`
77
- await createDirectory(pathVSCode)
78
- await copyPath(`${downloadPath}/vscode`, pathVSCode)
79
- }
80
-
81
- await removeDirectory(downloadPath)
82
- }
83
-
84
- function renamingJSON(directory, projectName) {
85
- const url = `${directory}/package.json`
86
- const txt = fs.readFileSync(url, { encoding: 'utf-8' })
87
- const obj = JSON.parse(txt)
88
-
89
- obj.name = projectName
90
-
91
- fs.writeFileSync(url, JSON.stringify(obj, null, 3))
92
- }
93
-
94
- async function download(packageName, directory) {
95
- try {
96
- await createDirectory(directory)
97
-
98
- const metadata = await npmRegistryFetch.json(packageName)
99
- const latestVersion = metadata['dist-tags'].latest
100
- const tarballURL = metadata.versions[latestVersion].dist.tarball
101
- const filename = tarballURL.substring(tarballURL.lastIndexOf('/') + 1)
102
- const tempFilePath = path.join(directory, filename)
103
-
104
- if (!fs.existsSync(directory))
105
- fs.mkdirSync(directory, { recursive: true })
106
-
107
- const response = await npmRegistryFetch(tarballURL)
108
-
109
- await new Promise((resolve, reject) => {
110
- const fileStream = fs.createWriteStream(tempFilePath)
111
- response.body.pipe(fileStream)
112
- response.body.on('error', reject)
113
- fileStream.on('finish', resolve)
114
- })
115
-
116
- await tar.x({ file: tempFilePath, cwd: directory, strip: 1 })
117
-
118
- fs.unlinkSync(tempFilePath)
119
- } catch (error) {
120
- console.error('downloadPackage')
121
- throw error
122
- }
123
- }
124
-
125
- function createDirectory(directory) {
126
- removeDirectory(directory)
127
- fs.mkdirSync(directory)
128
- }
129
-
130
- function removeDirectory(directory) {
131
- if (!fs.existsSync(directory)) return
132
- else fs.rmSync(directory, { recursive: true })
133
- }
134
-
135
- function copyPath(source, target) {
136
- if (!fs.existsSync(target)) fs.mkdirSync(target)
137
-
138
- fs.readdirSync(source).forEach(function(file) {
139
- const sourcePath = path.join(source, file)
140
- const targetPath = path.join(target, file)
141
- const statusPath = fs.lstatSync(sourcePath)
142
- const folderPath = statusPath.isDirectory()
143
-
144
- if (folderPath) copyPath(sourcePath, targetPath)
145
- else fs.copyFileSync(sourcePath, targetPath)
146
- })
147
- }
1
+ #!/usr/bin/env node
2
+
3
+ import npmRegistryFetch from 'npm-registry-fetch'
4
+ import { spawn , execSync } from 'child_process'
5
+ import inquirer from 'inquirer'
6
+ import path from 'path'
7
+ import * as tar from 'tar'
8
+ import fs from 'fs'
9
+
10
+ const CIANO = '\x1b[36m'
11
+ const GREEN = '\x1b[32m'
12
+ const RESET = '\x1b[0m'
13
+ const prefix = `${GREEN}+${RESET}`
14
+ const char = '─'
15
+ const line = char.repeat(51)
16
+ const decor = '-'.repeat(18)
17
+
18
+ const packageJson = fs.readFileSync('package.json', 'utf-8');
19
+ const { version } = JSON.parse(packageJson);
20
+
21
+ console.log('___________________________________________________')
22
+ console.log('')
23
+ console.log(` ${GREEN}REACTful${RESET} create `)
24
+ console.log(` ${CIANO}current version ${version}${RESET} `)
25
+ //console.log(`${decor}( ${GREEN}reactful${RESET}.js )${decor}`)
26
+ console.log('___________________________________________________')
27
+ // console.log(line)
28
+ console.log('')
29
+
30
+ const templates = ['empty', 'minimal', 'sampling']
31
+ const questions = [{
32
+ type: 'list',
33
+ name: 'template',
34
+ message: 'Which template?',
35
+ choices: templates,
36
+ prefix
37
+ },
38
+ { type: 'input', name: 'project', message: 'Project name?', prefix, default: 'Sample' },
39
+ { type: 'confirm', name: 'vscode', message: 'VS Code IDE?', prefix },
40
+ { type: 'confirm', name: 'install', message: 'Install?', prefix },
41
+ ]
42
+
43
+ inquirer.prompt(questions).then(prompting)
44
+
45
+ async function prompting(answers) {
46
+ console.log('- preparing...')
47
+
48
+ if (!answers.project) throw 'Required project name!'
49
+
50
+ const destination = path.join(process.cwd(), answers.project)
51
+ const downloadPath = path.join(destination, 'temp')
52
+
53
+ await createDirectory(destination)
54
+
55
+ console.log('- loading NPM...')
56
+ await download('@reactful/create', downloadPath)
57
+
58
+ console.log('- creating env...')
59
+ const env = `PORT=333\nMINIFY=FALSE`
60
+ fs.writeFileSync(`${destination}/.env`, env)
61
+
62
+ console.log('- copying template...')
63
+ await copyPath(`${downloadPath}/commons`, destination)
64
+ await copyPath(`${downloadPath}/templates/${answers.template}`, destination)
65
+
66
+ console.log('- renaming project...')
67
+ renamingJSON(destination, answers.project)
68
+
69
+ console.log('- installing dependencies...\n')
70
+ process.chdir(answers.project)
71
+ execSync(`bun install`)
72
+
73
+ if (answers.vscode) {
74
+ spawn("code", ['.'])
75
+
76
+ const pathVSCode = `${destination}/.vscode`
77
+ await createDirectory(pathVSCode)
78
+ await copyPath(`${downloadPath}/vscode`, pathVSCode)
79
+ }
80
+
81
+ await removeDirectory(downloadPath)
82
+ }
83
+
84
+ function renamingJSON(directory, projectName) {
85
+ const url = `${directory}/package.json`
86
+ const txt = fs.readFileSync(url, { encoding: 'utf-8' })
87
+ const obj = JSON.parse(txt)
88
+
89
+ obj.name = projectName
90
+
91
+ fs.writeFileSync(url, JSON.stringify(obj, null, 3))
92
+ }
93
+
94
+ async function download(packageName, directory) {
95
+ try {
96
+ await createDirectory(directory)
97
+
98
+ const metadata = await npmRegistryFetch.json(packageName)
99
+ const latestVersion = metadata['dist-tags'].latest
100
+ const tarballURL = metadata.versions[latestVersion].dist.tarball
101
+ const filename = tarballURL.substring(tarballURL.lastIndexOf('/') + 1)
102
+ const tempFilePath = path.join(directory, filename)
103
+
104
+ if (!fs.existsSync(directory))
105
+ fs.mkdirSync(directory, { recursive: true })
106
+
107
+ const response = await npmRegistryFetch(tarballURL)
108
+
109
+ await new Promise((resolve, reject) => {
110
+ const fileStream = fs.createWriteStream(tempFilePath)
111
+ response.body.pipe(fileStream)
112
+ response.body.on('error', reject)
113
+ fileStream.on('finish', resolve)
114
+ })
115
+
116
+ await tar.x({ file: tempFilePath, cwd: directory, strip: 1 })
117
+
118
+ fs.unlinkSync(tempFilePath)
119
+ } catch (error) {
120
+ console.error('downloadPackage')
121
+ throw error
122
+ }
123
+ }
124
+
125
+ function createDirectory(directory) {
126
+ removeDirectory(directory)
127
+ fs.mkdirSync(directory)
128
+ }
129
+
130
+ function removeDirectory(directory) {
131
+ if (!fs.existsSync(directory)) return
132
+ else fs.rmSync(directory, { recursive: true })
133
+ }
134
+
135
+ function copyPath(source, target) {
136
+ if (!fs.existsSync(target)) fs.mkdirSync(target)
137
+
138
+ fs.readdirSync(source).forEach(function(file) {
139
+ const sourcePath = path.join(source, file)
140
+ const targetPath = path.join(target, file)
141
+ const statusPath = fs.lstatSync(sourcePath)
142
+ const folderPath = statusPath.isDirectory()
143
+
144
+ if (folderPath) copyPath(sourcePath, targetPath)
145
+ else fs.copyFileSync(sourcePath, targetPath)
146
+ })
147
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reactful/create",
3
- "version": "1.2.19",
3
+ "version": "1.2.20",
4
4
  "main": "index.js",
5
5
  "type": "module",
6
6
  "description": "reactful scaffold",