NodeJs executes Linux scripts

(We can’t rot with the grass and trees in our life, can’t be drunk and dream of death, and live our lives in vain. We must make a difference. ——Fang Zhimin)

insert image description here

Why do you need to use NodeJs to execute Linux scripts

  1. Linux sh script commands are complicated to write. If you are not familiar with linux interactive commands, it is the most sensible choice to use a high-level programming language to execute
  2. The ecology of high-level languages ​​is relatively complete, and various ready-made toolkits can be used to complete script execution, such as npm of NodeJs, maven of Java, etc.
  3. Linux commands are relatively complicated, and usually require professional operation and maintenance to perform various complex command operations, but the use of high-level languages ​​shields the underlying commands of Linux, and directly uses the toolkit packaged in high-level languages

NodeJs executes script commands

Examples of application scenarios

For example, in the following scenarios, under the architecture of traditional k8s containerized services, dependencies need to be reinstalled every time a service is released, which can take two to three minutes at the fastest and ten minutes or more at the slowest. Without professional operation and maintenance, we can Use NodeJs to execute the command script. Before each release, compare the package.json files of the two versions. If they are inconsistent, install the dependencies again. Otherwise, skip the installation stage and use node_moodules in the current directory to publish. In the best case, you can save the time of dependent installation and greatly improve the speed of service release

scene details
Where did the previous version of node_modules/package.json come from?
  • In the case of not using containerization, we can copy the previous version of node_modules/package.json in the target directory of the build
  • In the case of containerization, we can directly use the docker image to generate a node_modules/package.json cache, and then add the new version of the Dockerfile, and use the same working directory to reuse

code example

// 在本地环境有nodejs的情况下 直接使用node xx.js执行即可
// 使用nodejs内置破快的exec执行linux命令
const {
    
     exec } = require('child_process');
// 本地的新json
const newJson = require('./package.json');
// 从上一个版本中复制来的旧json
const oldJson = require('./old.package.json');

// 安装依赖方法
const installDependencies = async (isInstall) => {
    
    
    return new Promise((resolve) =>{
    
    
		// 如果不需要安装则打印信息并跳出
        if(!isInstall){
    
    
            process.stdout.write('no need to install dependencies \n');
			// 结束promise
            return resolve();
        }
		// 执行yarn安装依赖命令
        const yarn = exec('yarn');
		// 实时监听该命令输入的信息并打印
        yarn.stdout.on('data', (data) => {
    
    
            process.stdout.write(data);
        });
		// 实时监听该命令的结束指令并打印
        yarn.stdout.on('end', () => {
    
    
            process.stdout.write('dependencies install complete \n');
            return resolve();
        })
		// 实时监听该命令的关闭指定并打印
        yarn.stdout.on('close', () => {
    
    
            process.stdout.write('dependencies install close \n');
            return resolve();
        });
    });
};

// 编译打包命令
const buildDist = async ()=>{
    
    
    return new Promise((resolve) =>{
    
    
		// 执行打包命令
        const yarn = exec('yarn run build');
        yarn.stdout.on('data', (data) => {
    
    
            process.stdout.write(data);
        });
        yarn.stdout.on('end', () => {
    
    
            process.stdout.write('build complete \n');
            return resolve();
        })
        yarn.stdout.on('close', () => {
    
    
            process.stdout.write('build close \n');
            return resolve();
        });
    });
};

// 立即执行函数,对比新旧两个依赖是否相同,然后执行依赖安装和打包编译操作
(async () => {
    
    

    const oldPro = oldJson.dependencies;
    const oldDev = oldJson.devDependencies;
    const oldProKeys = Object.keys(oldPro);
    const oldDevKeys = Object.keys(oldDev);

    const newPro = newJson.dependencies;
    const newdDev = newJson.devDependencies;
    const newProKeys = Object.keys(newPro);
    const newDevKeys = Object.keys(newdDev);

    let isInstall = false;

	// 对比新旧package.json的dependencies
    const loopProDependencies = () => {
    
    
        if (isInstall) {
    
    
            return;
        }
        /**
     * 对比新旧两个版本的安装包,出现以下情况的,需要重新安装依赖包
     * 1. 新的依赖在旧依赖中不存在的
     * 2. 新的依赖和旧依赖中版本号不同的
     */
        for (let i = 0; i < newProKeys.length; i++) {
    
    
            const prokey = newProKeys[i];
            const proKeyVersion = newPro[prokey];

            const oldKey = oldProKeys.find((v) => v === prokey);
            if (!oldKey) {
    
    
                process.stdout.write(`${
      
      prokey} no found, need install dependencies \n`);
                isInstall = true;
                break;
            }
            const oldKeyVersion = oldPro[oldKey];
            const isSameVersion = oldKeyVersion === proKeyVersion;
            if (!isSameVersion) {
    
    
                process.stdout.write(`${
      
      prokey} version difference, old_version:${
      
      oldKeyVersion} new_version:${
      
      proKeyVersion} \n`);
                isInstall = true;
                break;
            }
        }
    }

	// 对比新旧package.json的devDependencies
    const loopDevProDependencies = () => {
    
    
        if (isInstall) {
    
    
            return;
        }
        /**
     * 对比新旧两个版本的开发环境安装包,出现以下情况的,需要重新安装依赖包
     * 1. 新的依赖在旧依赖中不存在的
     * 2. 新的依赖和旧依赖中版本号不同的
     */
        for (let i = 0; i < newDevKeys.length; i++) {
    
    
            const devKey = newDevKeys[i];
            const devKeyVersion = newdDev[devKey];
            const oldKey = oldDevKeys.find((v) => v === devKey);
            if (!oldKey) {
    
    
                process.stdout.write(`${
      
      devKey} no found, need install dependencies \n`);
                isInstall = true;
                break;
            }
            const oldKeyVersion = oldDev[oldKey];
            const isSameVersion = oldKeyVersion === devKeyVersion;
            if (!isSameVersion) {
    
    
                process.stdout.write(`${
      
      devKey} version difference, old_version:${
      
      oldKeyVersion} new_version:${
      
      devKeyVersion} \n`);
                isInstall = true;
                break;
            }
        }
    }

    loopProDependencies();
    loopDevProDependencies();

    await installDependencies(isInstall);
    await buildDist();
})();






Guess you like

Origin blog.csdn.net/qq_42427109/article/details/132133767