UNPKG

498 BJavaScriptView Raw
1///@ts-check
2'use strict';
3const npm = require('./npm-dependency');
4const REG_VAR = /\{\{package.([\w\d]*?)\}\}/g;
5
6/**
7 * 替换变量 {{package.xx}} from `package.json`
8 * @param {object} obj
9 */
10function replaceVar(obj) {
11 const pkg = npm.loadPackage(process.cwd());
12 for (const k in obj) {
13 if (obj.hasOwnProperty(k) && typeof obj[k] === "string") {
14 obj[k] = obj[k].replace(REG_VAR, (m, key) => pkg[key]);
15 }
16 }
17 return obj;
18}
19
20module.exports = replaceVar
\No newline at end of file