@reactful/create 0.0.95 → 0.0.97

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. package/README.md +1 -2
  2. package/index.js +84 -79
  3. package/package.json +14 -8
package/README.md CHANGED
@@ -3,6 +3,5 @@
3
3
  Create a reactful framework with a project scafold tool.
4
4
 
5
5
  ```bash
6
- $ bun create reactful
7
- $ bunx creact-reactful
6
+ bunx @reactful/create@latest
8
7
  ```
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
- 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 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
- const download = async (subfolder, destination) =>
64
- downloadNPM('@reactful/create', subfolder, destination)
71
+ async function copyTemplate(temporaryPath, subdirectory, destination) {
72
+ try {
73
+ const folder = path.join(temporaryPath, subdirectory)
74
+ await copyFolder(folder, destination)
65
75
 
66
- async function downloadNPM(packageName, subdirectory, destination) {
67
- console.log(`- downloading ${subdirectory}...`)
76
+ } catch (error) {
77
+ console.error('copyTemplate')
78
+ throw error
79
+ }
80
+ }
68
81
 
82
+ async function download(packageName, directory) {
69
83
  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
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
- if (notFound) fs.mkdirSync(destination, { recursive: true })
95
+ const response = await npmRegistryFetch(tarballURL)
76
96
 
77
- fs.renameSync(from, destination)
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
- console.error('Download NPM:', error)
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
- if (!Array.isArray(contents) || !response.ok) {
92
- console.log('moment: DIRECTORY...')
93
- logging(response, contents)
94
- throw 'failed to download scafold from github...'
95
- }
114
+ function createDirectory(directory) {
115
+ removeDirectory(directory)
116
+ fs.mkdirSync(directory)
117
+ }
96
118
 
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
- }
119
+ function removeDirectory(directory) {
120
+ if (!fs.existsSync(directory)) return
121
+ else fs.rmSync(directory, { recursive: true })
123
122
  }
124
123
 
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)
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.95",
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
- "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
+ }