Created
April 26, 2019 09:22
-
-
Save giscafer/1d63c806a312bace2909fb2057b786ee to your computer and use it in GitHub Desktop.
CI配置,监听package.json 文件版本更新,然后脚本自动修改NODE_MODULES_VERSION版本号
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
'use strict'; | |
const fs = require('fs'); | |
const exec = require('child_process').exec; | |
const npmPackageName = process.env.npm_package_name; | |
const uri = './.gitlab-ci.yml'; | |
const uuid = () => { | |
return `${Math.random() | |
.toString(36) | |
.substr(2)}`; | |
}; | |
// 监听package.json 文件版本更新,然后脚本自动修改NODE_MODULES_VERSION版本号 | |
exec('git diff --cached --name-only package.json', function(error, stdout, stderr) { | |
const regexStr = `NODE_MODULES_VERSION: '${npmPackageName}-(.*?)'`; | |
const regex = new RegExp(regexStr, 'g'); | |
let newData = ''; | |
if (error) { | |
console.error('error: ' + error); | |
return; | |
} | |
console.log('git diff stdout: ', stdout); | |
if (!stdout) { | |
return console.log('package.json 无修改'); | |
} | |
try { | |
let oldData = fs.readFileSync(uri, { encoding: 'utf-8' }); | |
newData = oldData.replace(regex, `NODE_MODULES_VERSION: '${npmPackageName}-${uuid()}'`); | |
} catch (error) { | |
console.log('Error: 读取失败!'); | |
return; | |
} | |
try { | |
fs.writeFileSync(uri, newData); | |
exec('git add .gitlab-ci.yml', function(error, stdout, stderr) { | |
if (error) { | |
console.error('git add error: ' + error); | |
return; | |
} | |
console.log(`git add ${uri} success`); | |
}); | |
console.log(`Success: ${uri}文件修改成功!`); | |
} catch (e) { | |
console.log('Error: 文件:修改失败!'); | |
return; | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment