1.21.1 • Published 1 year ago

clean-scripts v1.21.1

Weekly downloads
150
License
MIT
Repository
github
Last release
1 year ago

clean-scripts

Dependency Status devDependency Status Build Status: Windows Github CI npm version Downloads type-coverage

A CLI tool to make scripts in package.json clean.

install

yarn global add clean-scripts

usage

create config file(named clean-scripts.config.js or something else) like:

module.exports = {
    build: "tsc",
    lint: "tslint index.ts"
}

run clean-scripts build, or clean-scripts lint

or clean-scripts build --config clean-scripts.config.js

or clean-scripts build --config clean-scripts.config.ts

options

keydescription
--configconfig file
-h,--helpPrint this message.
-v,--versionPrint the version

features

string script

module.exports = {
    build: "tsc"
}

array script

  • executed one by one with order
  • used when later script depends on previous script's success
module.exports = {
    build: [
        "rimraf dist",
        "tsc"
    ]
}

Set or Object script

  • executed collaterally without order
  • used when the scripts are irrelated
  • they are all started at first time, when they are all done, it's a success, otherwise exit current process
module.exports = {
    build: {
        js: `tsc`,
        css: `cleancss -o index.bundle.css index.css`
    }
}

nested script

module.exports = {
    build: [
        "rimraf dist",
        {
            js: `tsc`,
            css: [
                `lessc index.less > index.css`,
                `cleancss -o index.bundle.css index.css`
            ]
        }
    ]
}

custom function script

the type of the function should be (context: { [key: string]: any }, parameters: string[]) => Promise<void | { name?: string times?: Time[] script?: Script }>

module.exports = {
    build: () => new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve()
        }, 1000)
    }),
    test: async () => {
        // todo
    }
}

The context can be used to transfer data between different scripts.

module.exports = {
    build: [
        context => {
            context.foo = 'abc'
            return Promise.resolve()
        },
        context => {
            console.log(context.foo) // 'abc'
            return Promise.resolve()
        }
    ]
}

The parameters can be passed from CLI parameters

module.exports = {
    build: (context, parameters) => {
        console.log(parameters) // run `clean-scripts build foo bar`, got `["foo", "bar"]`
        return Promise.resolve()
    }
}

Custom function return value will also be executed.

child script

module.exports = {
    build: [
        `rimraf dist/`,
        `tsc -p src/`
    ],
    lint: {
        ts: `tslint "src/**/*.ts"`,
        js: `standard "**/*.config.js"`
    }
}
  • run clean-scripts build[0] to run rimraf dist/
  • run clean-scripts lint.ts to run tslint "src/**/*.ts"

start service

const { Service } = require('clean-scripts')

module.exports = {
  build: [
    new Service('http-server -p 8000'),
    new Service('http-server', 'server2'), // the child process can be accessed by `context.server2` later
    new Service('http-server -p 8000', { maximumCpu: 50, maximumMemory: 1175552 }), // if the cpu usage of this service > maximumCpu, throw an error. same to the memory usage
  ]
}

All services will be killed(send SIGINT actually) after all scripts end, or any script errors.

The cpu and memory check runs every 1 second.

start program

const { Program } = require('clean-scripts')

module.exports = {
  build: [
    new Program('http-server -p 8000', 10000), // the program will last at most 10 seconds, can be used to test the start process of a program
    new Program('http-server -p 8000', 10000, { maximumCpu: 50, maximumMemory: 1175552 }), // if the cpu usage of this program > maximumCpu, throw an error. same to the memory usage
  ]
}

A program will be killed(send SIGINT actually) after the script end.

The cpu and memory check runs every 1 second.

tasks

const { Tasks } = require('clean-scripts')

module.exports = {
    build: new Tasks([
      {
        name: 'build a',
        script: 'yarn workspace a run build'
      },
      {
        name: 'test a',
        script: 'yarn workspace a run test',
        dependencies: [
          'build a'
        ]
      },
      {
        name: 'build b',
        script: 'yarn workspace b run build',
        dependencies: [
          'build a'
        ]
      },
      {
        name: 'test b',
        script: 'yarn workspace b run test',
        dependencies: [
          'build b'
        ]
      }
    ])
}

the 4 tasks will be execuated in following order:

  1. build a
  2. build b and test a
  3. test b as soon as build b completed

This can be very useful and effective for complex or dynamic tasks.

short-hand methods

const { sleep, readableStreamEnd, execAsync, executeScriptAsync, checkGitStatus } = require('clean-scripts')

module.exports = {
  build: [
    () => sleep(5000), // sleep milliseconds
    async () => {
        const readable = getReadableStreamSomehow()
        readable.on('data', chunk => {
            console.log(`Received ${chunk.length} bytes of data.`)
        })
        await readableStreamEnd(readable) // wait readable stream ends
    },
    async () => {
        const { stdout } = await execAsync('git status -s') // promisified `childProcess.exec`
        if (stdout) {
            console.log(stdout)
            throw new Error(`generated files doesn't match.`)
        }
    },
    async () => {
        await executeScriptAsync([ // support string script, array script, child script, nested script and so on
            `rimraf dist/`,
            `tsc -p src/`
        ])
    },
    () => checkGitStatus() // check git status
  ]
}
1.21.0

1 year ago

1.21.1

1 year ago

1.20.2

3 years ago

1.20.1

3 years ago

1.20.0

3 years ago

1.19.0

3 years ago

1.18.7

4 years ago

1.18.6

4 years ago

1.18.5

4 years ago

1.18.4

4 years ago

1.18.1

4 years ago

1.18.3

4 years ago

1.18.2

4 years ago

1.18.0

4 years ago

1.17.1

4 years ago

1.17.0

4 years ago

1.16.1

4 years ago

1.16.0

4 years ago

1.15.0

4 years ago

1.14.0

4 years ago

1.13.1

4 years ago

1.13.0

4 years ago

1.12.3

4 years ago

1.12.2

4 years ago

1.12.1

5 years ago

1.12.0

5 years ago

1.11.2

5 years ago

1.11.1

5 years ago

1.11.0

5 years ago

1.10.0

5 years ago

1.9.2

6 years ago

1.9.1

6 years ago

1.9.0

6 years ago

1.9.0-alpha.3

6 years ago

1.9.0-alpha.2

6 years ago

1.9.0-alpha.1

6 years ago

1.9.0-alpha.0

6 years ago

1.8.1

6 years ago

1.8.0

6 years ago

1.7.0

6 years ago

1.6.0

7 years ago

1.5.2

7 years ago

1.5.1

7 years ago

1.5.0

7 years ago

1.4.2

7 years ago

1.4.1

7 years ago

1.4.0

7 years ago

1.3.3

7 years ago

1.3.2

7 years ago

1.3.1

7 years ago

1.3.0

7 years ago

1.2.6

7 years ago

1.2.5

7 years ago

1.2.4

7 years ago

1.2.3

7 years ago

1.2.2

7 years ago

1.2.1

7 years ago

1.2.0

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago