Skip to content

Commit 13ee3f8

Browse files
committed
feat: download template support generate package.json with user input
1 parent 4a42e9b commit 13ee3f8

File tree

5 files changed

+77
-9
lines changed

5 files changed

+77
-9
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"dependencies": {
3737
"@vue/repl": "^1.0.0",
3838
"file-saver": "^2.0.5",
39-
"jszip": "^3.6.0",
39+
"jszip": "^3.9.1",
4040
"vue": "3.2.31"
4141
},
4242
"devDependencies": {

pnpm-lock.yaml

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/download/download.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,21 @@ import pkg from './template/package.json?raw'
66
import config from './template/vite.config.js?raw'
77
import readme from './template/README.md?raw'
88

9-
export async function downloadProject (store: any) {
9+
import { handlePackageJson } from './utils'
10+
import type { ReplStore } from 'src/store'
11+
12+
export async function downloadProject (store: ReplStore) {
13+
console.warn('pkg', handlePackageJson(pkg, store))
1014
if (!confirm('Download project files?')) {
1115
return
1216
}
1317

1418
const { default: JSZip } = await import('jszip')
1519
const zip = new JSZip()
16-
20+
console.warn('pkg', pkg)
1721
// basic structure
1822
zip.file('index.html', index)
19-
zip.file('package.json', pkg)
23+
zip.file('package.json', handlePackageJson(pkg, store))
2024
zip.file('vite.config.js', config)
2125
zip.file('README.md', readme)
2226

src/download/template/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
{
2-
"name": "vite-vant-starter",
2+
"name": "vant-starter",
33
"version": "0.0.0",
44
"scripts": {
55
"build": "vite build",
66
"dev": "vite",
77
"serve": "vite preview"
88
},
99
"dependencies": {
10+
"@vant/popperjs": "1.1.0",
11+
"@vant/touch-emulator": "1.3.2",
12+
"@vant/use": "1.3.6",
1013
"vant": "^3.4.6",
1114
"vue": "^3.2.0"
1215
},

src/download/utils.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import type { ReplStore } from 'src/store'
2+
3+
const DEFAULT_VERSION = '*'
4+
5+
/**
6+
* get version map form importMap,
7+
*
8+
* e.g. {
9+
*
10+
* "vue": "https://cdn.jsdelivr.net/npm/@vue/[email protected]/dist/runtime-dom.esm-browser.js",
11+
12+
* "@vant/use": "https://cdn.jsdelivr.net/npm/@vant/use/dist/index.esm.js"
13+
*
14+
* }
15+
*
16+
* @returns { "vue": "3.2.31", "@vant/use": "*" }
17+
*/
18+
export function getPackageVersionMap (imports: Record<string, string>) {
19+
return Object.entries(imports).reduce((resultImportMap, [pkgName, pkgPath]) => {
20+
const version = `${pkgPath}/`.match(/@((?=\d).+?)(?=\/)/)?.[1]
21+
resultImportMap[pkgName] = version ?? DEFAULT_VERSION
22+
return resultImportMap
23+
}, {} as Record<string, string>)
24+
}
25+
26+
interface PackageJsonType {
27+
'name': string,
28+
'version': string,
29+
'dependencies': Record<string, string>,
30+
'devDependencies': Record<string, string>,
31+
[key: string]: any
32+
}
33+
34+
/**
35+
* generate package.json with user input
36+
* @param pckJsonStr package.json stringify
37+
* @param store {ReplStore}
38+
* @returns
39+
*/
40+
export function handlePackageJson (pckJsonStr: string, store: ReplStore) {
41+
let pkgJson: Partial<PackageJsonType> = {}
42+
try {
43+
pkgJson = JSON.parse(pckJsonStr)
44+
} catch (error) {
45+
console.warn('parse package.json error:', error)
46+
}
47+
48+
const imports = store.getImportMap()?.imports
49+
if (imports) {
50+
const pkgVersionMap = getPackageVersionMap(imports)
51+
52+
Object.entries(pkgVersionMap).forEach(([pkgName, version]) => {
53+
pkgJson.dependencies = {
54+
...pkgJson.dependencies,
55+
[pkgName]: version !== DEFAULT_VERSION ? version : pkgJson.dependencies?.[pkgName] ?? version
56+
}
57+
})
58+
}
59+
60+
return JSON.stringify(pkgJson, null, 2)
61+
}

0 commit comments

Comments
 (0)