-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: download template support generate package.json with user input
- Loading branch information
1 parent
4a42e9b
commit 13ee3f8
Showing
5 changed files
with
77 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import type { ReplStore } from 'src/store' | ||
|
||
const DEFAULT_VERSION = '*' | ||
|
||
/** | ||
* get version map form importMap, | ||
* | ||
* e.g. { | ||
* | ||
* "vue": "https://cdn.jsdelivr.net/npm/@vue/[email protected]/dist/runtime-dom.esm-browser.js", | ||
* "@vant/use": "https://cdn.jsdelivr.net/npm/@vant/use/dist/index.esm.js" | ||
* | ||
* } | ||
* | ||
* @returns { "vue": "3.2.31", "@vant/use": "*" } | ||
*/ | ||
export function getPackageVersionMap (imports: Record<string, string>) { | ||
return Object.entries(imports).reduce((resultImportMap, [pkgName, pkgPath]) => { | ||
const version = `${pkgPath}/`.match(/@((?=\d).+?)(?=\/)/)?.[1] | ||
resultImportMap[pkgName] = version ?? DEFAULT_VERSION | ||
return resultImportMap | ||
}, {} as Record<string, string>) | ||
} | ||
|
||
interface PackageJsonType { | ||
'name': string, | ||
'version': string, | ||
'dependencies': Record<string, string>, | ||
'devDependencies': Record<string, string>, | ||
[key: string]: any | ||
} | ||
|
||
/** | ||
* generate package.json with user input | ||
* @param pckJsonStr package.json stringify | ||
* @param store {ReplStore} | ||
* @returns | ||
*/ | ||
export function handlePackageJson (pckJsonStr: string, store: ReplStore) { | ||
let pkgJson: Partial<PackageJsonType> = {} | ||
try { | ||
pkgJson = JSON.parse(pckJsonStr) | ||
} catch (error) { | ||
console.warn('parse package.json error:', error) | ||
} | ||
|
||
const imports = store.getImportMap()?.imports | ||
if (imports) { | ||
const pkgVersionMap = getPackageVersionMap(imports) | ||
|
||
Object.entries(pkgVersionMap).forEach(([pkgName, version]) => { | ||
pkgJson.dependencies = { | ||
...pkgJson.dependencies, | ||
[pkgName]: version !== DEFAULT_VERSION ? version : pkgJson.dependencies?.[pkgName] ?? version | ||
} | ||
}) | ||
} | ||
|
||
return JSON.stringify(pkgJson, null, 2) | ||
} |