Thanks to visit codestin.com
Credit goes to github.com

Skip to content

But why? #923

@ghost

Description

Can you enlighten me why we need this? I am fond of the don't try to reinvent the wheel principle, and I understand that for command line it is better to have (cryptic) abbreviations, because it is faster to type, but if we are writing code in a text file, we usually have auto completion, so we could easily write something readable, which does not have to be short.

If I take your example literally:

var shell = require('shelljs');

if (!shell.which('git')) {
    shell.echo('Sorry, this script requires git');
    shell.exit(1);
}

// Copy files to release dir
shell.rm('-rf', 'out/Release');
shell.cp('-R', 'stuff/', 'out/Release');

// Replace macros in each .js file
shell.cd('lib');
shell.ls('*.js').forEach(function (file) {
    shell.sed('-i', 'BUILD_VERSION', 'v0.1.2', file);
    shell.sed('-i', /^.*REMOVE_THIS_LINE.*$/, '', file);
    shell.sed('-i', /.*REPLACE_LINE_WITH_MACRO.*\n/, shell.cat('macro.js'), file);
});
shell.cd('..');

// Run external tool synchronously
if (shell.exec('git commit -am "Auto-commit"').code !== 0) {
    shell.echo('Error: Git commit failed');
    shell.exit(1);
}

then it is not that hard to write something human readable based on it:

const shell = require('shelljs');
shell.install({git: require("shelljs-git")});

class MyTask extends shell.Task {
    execute(){
        this.gitRequired();
        this.copyFilesToRelaseDirectory();
        this.replaceMacrosInEachJavascriptFile();
        this.runExternalToolSynchronously();
    }

    gitRequired(){
        if (!this.which('git'))
            throw new Error('Sorry, this script requires git');
    }

    copyFilesToRelaseDirectory(){
        this.remove({target: 'out/Release', recursive: true, force: true});
        this.copy({source: 'stuff/', target: 'out/Release', recursive: true});
    }
    
    replaceMacrosInEachJavascriptFile(){
        const searchReplacementPairs = [
            {search: 'BUILD_VERSION', replacement: 'v0.1.2'},
            {search: /^.*REMOVE_THIS_LINE.*$/, replacement: ''},
            {search: /.*REPLACE_LINE_WITH_MACRO.*\n/, replacement: this.readFile('macro.js')}
        ];
        this.changeDirectory('lib');
        this.list('*.js').forEach((file) => {
            searchReplacementPairs.forEach((pair) => {
                this.streamEditor({source: file, search: pair.search, replacement: pair.replacement});
            });
        });
        this.changeDirectory('..');
    }
    
    runExternalToolSynchronously(){
        try {
            this.git.commit({all: true, message: "Auto-commit"});
        } catch(error){
            throw new Error("Git commit failed");
        }
    }
}

const myTask = new MyTask();
myTask.execute();

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionQuestion from a user (which may not require code/documentation changes to the project)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions