@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.
Files changed (2) hide show
  1. package/index.js +84 -80
  2. 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
- type: 'list',
27
- name: 'template',
28
- message: 'Which template?',
29
- choices: templates,
30
- prefix
31
- },
32
- { type: 'input', name: 'project', message: 'Project name?', prefix },
33
- { type: 'confirm', name: 'vscode', message: 'VS Code IDE?', prefix },
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(async function (answers) {
38
- fs.mkdirSync(answers.project)
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
- await download(`commons`, destination)
43
- await download(`templates/${answers.template}`, destination)
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
- const download = async (subfolder, destination) =>
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
- const pkg = npmPackage.resolve(packageName)
71
- const content = await npmDownload(pkg, { dir: 'temp' })
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
- console.error('Download NPM:', error)
76
+ console.error('copyTemplate')
77
+ throw error
80
78
  }
81
79
  }
82
80
 
83
- async function downloadGitHub(user, repository, subdir, destination) {
84
- console.log(`- downloading ${subdir}...`)
81
+ async function download(packageName, directory) {
82
+ try {
83
+ await createDirectory(directory)
85
84
 
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()
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
- if (!Array.isArray(contents) || !response.ok) {
92
- console.log('moment: DIRECTORY...')
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
- for (const item of contents) {
98
- if (item.type === 'dir' && item.name) {
99
- const from = `${subdir}/${item.name}`
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
- }
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
- function logging(response, contents, download) {
126
- console.log('url: ', response.url)
127
- console.log('code: ', response.status)
128
- console.log('array: ', Array.isArray(contents))
129
- console.log('textual: ', JSON.stringify(contents))
130
- console.log('download: ', item.download_url)
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.94",
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
- "scripts": { "deploy": "npm publish --access public" },
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
- "node-fetch": "^3.3.2",
17
- "npm-download": "^2.0.0",
18
- "npm-package-arg": "^11.0.1"
23
+ "npm-registry-fetch": "^16.1.0",
24
+ "tar": "^6.2.0"
19
25
  }
20
- }
26
+ }