@reactful/create 0.0.95 → 0.0.96

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.
Files changed (2) hide show
  1. package/index.js +83 -79
  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'
@@ -22,33 +20,42 @@ console.log(line)
22
20
  const templates = ['empty', 'minimal', 'sampling']
23
21
 
24
22
  const questions = [{
25
- type: 'list',
26
- name: 'template',
27
- message: 'Which template?',
28
- choices: templates,
29
- prefix
30
- },
31
- { type: 'input', name: 'project', message: 'Project name?', prefix },
32
- { type: 'confirm', name: 'vscode', message: 'VS Code IDE?', prefix },
33
- { type: 'confirm', name: 'install', message: 'Install?', prefix },
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(async function (answers) {
37
- fs.mkdirSync(answers.project)
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
- await download(`commons`, destination)
42
- await download(`templates/${answers.template}`, destination)
43
-
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
+
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}`)
50
57
  execSync(`bun install`)
51
- })
58
+ }
52
59
 
53
60
  function renamingJSON(directory, projectName) {
54
61
  const url = `${directory}/package.json`
@@ -60,72 +67,69 @@ function renamingJSON(directory, projectName) {
60
67
  fs.writeFileSync(url, JSON.stringify(obj, null, 3))
61
68
  }
62
69
 
63
- const download = async (subfolder, destination) =>
64
- downloadNPM('@reactful/create', subfolder, destination)
70
+ async function copyTemplate(temporaryPath, subdirectory, destination) {
71
+ try {
72
+ const folder = path.join(temporaryPath, subdirectory)
73
+ await copyFolder(folder, destination)
65
74
 
66
- async function downloadNPM(packageName, subdirectory, destination) {
67
- console.log(`- downloading ${subdirectory}...`)
75
+ } catch (error) {
76
+ console.error('copyTemplate')
77
+ throw error
78
+ }
79
+ }
68
80
 
81
+ async function download(packageName, directory) {
69
82
  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
83
+ await createDirectory(directory)
84
+
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
+
91
+ if (!fs.existsSync(directory))
92
+ fs.mkdirSync(directory, { recursive: true })
74
93
 
75
- if (notFound) fs.mkdirSync(destination, { recursive: true })
94
+ const response = await npmRegistryFetch(tarballURL)
76
95
 
77
- fs.renameSync(from, destination)
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)
78
106
  } catch (error) {
79
- console.error('Download NPM:', error)
107
+ console.error('downloadPackage')
108
+ throw error
80
109
  }
81
110
  }
82
111
 
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
112
 
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
- }
113
+ function createDirectory(directory) {
114
+ removeDirectory(directory)
115
+ fs.mkdirSync(directory)
116
+ }
96
117
 
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
- }
122
- }
118
+ function removeDirectory(directory) {
119
+ if (!fs.existsSync(directory)) return
120
+ else fs.rmSync(directory, { recursive: true })
123
121
  }
124
122
 
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)
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.95",
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
+ }