├── .github └── workflows │ └── nodejs.yml ├── .gitignore ├── .prettierignore ├── .prettierrc ├── LICENSE ├── README.md ├── package-lock.json ├── package.json ├── rollup.config.js ├── src ├── command.ts ├── index.ts └── run.ts ├── test-colors.js ├── test.ts └── tsconfig.json /.github/workflows/nodejs.yml: -------------------------------------------------------------------------------- 1 | name: Node CI 2 | 3 | on: [push] 4 | 5 | jobs: 6 | build: 7 | runs-on: ubuntu-latest 8 | 9 | strategy: 10 | matrix: 11 | node-version: [10.x, 12.x] 12 | 13 | steps: 14 | - uses: actions/checkout@v1 15 | - name: Use Node.js ${{ matrix.node-version }} 16 | uses: actions/setup-node@v1 17 | with: 18 | node-version: ${{ matrix.node-version }} 19 | - name: test 20 | run: | 21 | npm install 22 | npm run test:bare 23 | npm run lint:test 24 | npm run build 25 | env: 26 | CI: true 27 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | dist 3 | typings 4 | -------------------------------------------------------------------------------- /.prettierignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .eslintignore 3 | .gitignore 4 | .prettierignore 5 | LICENSE 6 | dist 7 | typings 8 | -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "arrowParens": "always", 3 | "bracketSpacing": true, 4 | "endOfLine": "lf", 5 | "htmlWhitespaceSensitivity": "css", 6 | "insertPragma": false, 7 | "printWidth": 120, 8 | "proseWrap": "preserve", 9 | "requirePragma": false, 10 | "semi": true, 11 | "singleQuote": true, 12 | "tabWidth": 4, 13 | "trailingComma": "es5", 14 | "useTabs": true 15 | } 16 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Elijah Mooring 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # rollup-plugin-command 2 | 3 | Run commands and call functions when bundles are generated 4 | 5 | ## Installation 6 | 7 | ```sh 8 | npm i -D rollup-plugin-command 9 | ``` 10 | 11 | ## Usage 12 | 13 | ```js 14 | // rollup.config.js 15 | import command from 'rollup-plugin-command'; 16 | 17 | export default { 18 | // ... 19 | plugins: [ 20 | // ... 21 | command(`node tests.js`, options), 22 | ], 23 | // ... 24 | }; 25 | ``` 26 | 27 | The [options](#options) are, of course, optional. 28 | 29 | ```js 30 | command(require('tests.js')); 31 | ``` 32 | 33 | ```js 34 | command( 35 | [ 36 | `npm test`, // The next command will not be executed until this 37 | // one is finished. 38 | 39 | require('./scripts/cleanup').someFunc, // If this returns a 40 | // promise, and `options.wait` is true (it's false by default), this 41 | // plugin will wait for it to be resolved before moving on to the 42 | // next command or finishing the build. 43 | ], 44 | { exitOnFail: true } 45 | ); // Default for options.exitOnFail is false. 46 | ``` 47 | 48 | ### options 49 | 50 | ```ts 51 | interface CommandOptions { 52 | exitOnFail?: boolean; // (Only applies when one of the given commands 53 | // is a string) Exit the current process when the child process fails. 54 | // Default is false. 55 | 56 | once?: boolean; // (Only valid when rollup is in watch mode) If the 57 | // commands should be executed only the first time a bundle is built. 58 | // Default is false. 59 | 60 | wait?: boolean; // If the the build should wait for the commands to 61 | // be executed. Default is false. 62 | } 63 | ``` 64 | 65 | I hope you find this package usefull! 66 | 67 | ## License 68 | 69 | [MIT](/LICENSE) 70 | -------------------------------------------------------------------------------- /package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "rollup-plugin-command", 3 | "version": "1.1.3", 4 | "lockfileVersion": 1, 5 | "requires": true, 6 | "dependencies": { 7 | "@types/color-name": { 8 | "version": "1.1.1", 9 | "resolved": "https://registry.npmjs.org/@types/color-name/-/color-name-1.1.1.tgz", 10 | "integrity": "sha512-rr+OQyAjxze7GgWrSaJwydHStIhHq2lvY3BOC2Mj7KnzI7XK0Uw1TOOdI9lDoajEbSWLiYgoo4f1R51erQfhPQ==", 11 | "dev": true 12 | }, 13 | "@types/estree": { 14 | "version": "0.0.39", 15 | "resolved": "https://registry.npmjs.org/@types/estree/-/estree-0.0.39.tgz", 16 | "integrity": "sha512-EYNwp3bU+98cpU4lAWYYL7Zz+2gryWH1qbdDTidVd6hkiR6weksdbMadyXKXNPEkQFhXM+hVO9ZygomHXp+AIw==", 17 | "dev": true 18 | }, 19 | "@types/node": { 20 | "version": "12.12.5", 21 | "resolved": "https://registry.npmjs.org/@types/node/-/node-12.12.5.tgz", 22 | "integrity": "sha512-KEjODidV4XYUlJBF3XdjSH5FWoMCtO0utnhtdLf1AgeuZLOrRbvmU/gaRCVg7ZaQDjVf3l84egiY0mRNe5xE4A==", 23 | "dev": true 24 | }, 25 | "@types/resolve": { 26 | "version": "0.0.8", 27 | "resolved": "https://registry.npmjs.org/@types/resolve/-/resolve-0.0.8.tgz", 28 | "integrity": "sha512-auApPaJf3NPfe18hSoJkp8EbZzer2ISk7o8mCC3M9he/a04+gbMF97NkpD2S8riMGvm4BMRI59/SZQSaLTKpsQ==", 29 | "dev": true, 30 | "requires": { 31 | "@types/node": "*" 32 | } 33 | }, 34 | "acorn": { 35 | "version": "7.2.0", 36 | "resolved": "https://registry.npmjs.org/acorn/-/acorn-7.2.0.tgz", 37 | "integrity": "sha512-apwXVmYVpQ34m/i71vrApRrRKCWQnZZF1+npOD0WV5xZFfwWOmKGQ2RWlfdy9vWITsenisM8M0Qeq8agcFHNiQ==", 38 | "dev": true 39 | }, 40 | "ansi-styles": { 41 | "version": "4.2.1", 42 | "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.2.1.tgz", 43 | "integrity": "sha512-9VGjrMsG1vePxcSweQsN20KY/c4zN0h9fLjqAbwbPfahM3t+NL+M9HC8xeXG2I8pX5NoamTGNuomEUFI7fcUjA==", 44 | "dev": true, 45 | "requires": { 46 | "@types/color-name": "^1.1.1", 47 | "color-convert": "^2.0.1" 48 | } 49 | }, 50 | "builtin-modules": { 51 | "version": "3.1.0", 52 | "resolved": "https://registry.npmjs.org/builtin-modules/-/builtin-modules-3.1.0.tgz", 53 | "integrity": "sha512-k0KL0aWZuBt2lrxrcASWDfwOLMnodeQjodT/1SxEQAXsHANgo6ZC/VEaSEHCXt7aSTZ4/4H5LKa+tBXmW7Vtvw==", 54 | "dev": true 55 | }, 56 | "chalk": { 57 | "version": "4.0.0", 58 | "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.0.0.tgz", 59 | "integrity": "sha512-N9oWFcegS0sFr9oh1oz2d7Npos6vNoWW9HvtCg5N1KRFpUhaAhvTv5Y58g880fZaEYSNm3qDz8SU1UrGvp+n7A==", 60 | "dev": true, 61 | "requires": { 62 | "ansi-styles": "^4.1.0", 63 | "supports-color": "^7.1.0" 64 | } 65 | }, 66 | "color-convert": { 67 | "version": "2.0.1", 68 | "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", 69 | "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==", 70 | "dev": true, 71 | "requires": { 72 | "color-name": "~1.1.4" 73 | } 74 | }, 75 | "color-name": { 76 | "version": "1.1.4", 77 | "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", 78 | "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==", 79 | "dev": true 80 | }, 81 | "cross-env": { 82 | "version": "7.0.2", 83 | "resolved": "https://registry.npmjs.org/cross-env/-/cross-env-7.0.2.tgz", 84 | "integrity": "sha512-KZP/bMEOJEDCkDQAyRhu3RL2ZO/SUVrxQVI0G3YEQ+OLbRA3c6zgixe8Mq8a/z7+HKlNEjo8oiLUs8iRijY2Rw==", 85 | "dev": true, 86 | "requires": { 87 | "cross-spawn": "^7.0.1" 88 | } 89 | }, 90 | "cross-spawn": { 91 | "version": "7.0.3", 92 | "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz", 93 | "integrity": "sha512-iRDPJKUPVEND7dHPO8rkbOnPpyDygcDFtWjpeWNCgy8WP2rXcxXL8TskReQl6OrB2G7+UJrags1q15Fudc7G6w==", 94 | "dev": true, 95 | "requires": { 96 | "path-key": "^3.1.0", 97 | "shebang-command": "^2.0.0", 98 | "which": "^2.0.1" 99 | } 100 | }, 101 | "estree-walker": { 102 | "version": "0.6.1", 103 | "resolved": "https://registry.npmjs.org/estree-walker/-/estree-walker-0.6.1.tgz", 104 | "integrity": "sha512-SqmZANLWS0mnatqbSfRP5g8OXZC12Fgg1IwNtLsyHDzJizORW4khDfjPqJZsemPWBB2uqykUah5YpQ6epsqC/w==", 105 | "dev": true 106 | }, 107 | "fsevents": { 108 | "version": "2.1.3", 109 | "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.1.3.tgz", 110 | "integrity": "sha512-Auw9a4AxqWpa9GUfj370BMPzzyncfBABW8Mab7BGWBYDj4Isgq+cDKtx0i6u9jcX9pQDnswsaaOTgTmA5pEjuQ==", 111 | "dev": true, 112 | "optional": true 113 | }, 114 | "has-flag": { 115 | "version": "4.0.0", 116 | "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", 117 | "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==", 118 | "dev": true 119 | }, 120 | "is-module": { 121 | "version": "1.0.0", 122 | "resolved": "https://registry.npmjs.org/is-module/-/is-module-1.0.0.tgz", 123 | "integrity": "sha1-Mlj7afeMFNW4FdZkM2tM/7ZEFZE=", 124 | "dev": true 125 | }, 126 | "is-reference": { 127 | "version": "1.1.4", 128 | "resolved": "https://registry.npmjs.org/is-reference/-/is-reference-1.1.4.tgz", 129 | "integrity": "sha512-uJA/CDPO3Tao3GTrxYn6AwkM4nUPJiGGYu5+cB8qbC7WGFlrKZbiRo7SFKxUAEpFUfiHofWCXBUNhvYJMh+6zw==", 130 | "dev": true, 131 | "requires": { 132 | "@types/estree": "0.0.39" 133 | } 134 | }, 135 | "isexe": { 136 | "version": "2.0.0", 137 | "resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz", 138 | "integrity": "sha1-6PvzdNxVb/iUehDcsFctYz8s+hA=", 139 | "dev": true 140 | }, 141 | "magic-string": { 142 | "version": "0.25.4", 143 | "resolved": "https://registry.npmjs.org/magic-string/-/magic-string-0.25.4.tgz", 144 | "integrity": "sha512-oycWO9nEVAP2RVPbIoDoA4Y7LFIJ3xRYov93gAyJhZkET1tNuB0u7uWkZS2LpBWTJUWnmau/To8ECWRC+jKNfw==", 145 | "dev": true, 146 | "requires": { 147 | "sourcemap-codec": "^1.4.4" 148 | } 149 | }, 150 | "path-key": { 151 | "version": "3.1.1", 152 | "resolved": "https://registry.npmjs.org/path-key/-/path-key-3.1.1.tgz", 153 | "integrity": "sha512-ojmeN0qd+y0jszEtoY48r0Peq5dwMEkIlCOu6Q5f41lfkswXuKtYrhgoTpLnyIcHm24Uhqx+5Tqm2InSwLhE6Q==", 154 | "dev": true 155 | }, 156 | "path-parse": { 157 | "version": "1.0.6", 158 | "resolved": "https://registry.npmjs.org/path-parse/-/path-parse-1.0.6.tgz", 159 | "integrity": "sha512-GSmOT2EbHrINBf9SR7CDELwlJ8AENk3Qn7OikK4nFYAu3Ote2+JYNVvkpAEQm3/TLNEJFD/xZJjzyxg3KBWOzw==", 160 | "dev": true 161 | }, 162 | "prettier": { 163 | "version": "2.0.5", 164 | "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.0.5.tgz", 165 | "integrity": "sha512-7PtVymN48hGcO4fGjybyBSIWDsLU4H4XlvOHfq91pz9kkGlonzwTfYkaIEwiRg/dAJF9YlbsduBAgtYLi+8cFg==", 166 | "dev": true 167 | }, 168 | "resolve": { 169 | "version": "1.12.0", 170 | "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.12.0.tgz", 171 | "integrity": "sha512-B/dOmuoAik5bKcD6s6nXDCjzUKnaDvdkRyAk6rsmsKLipWj4797iothd7jmmUhWTfinVMU+wc56rYKsit2Qy4w==", 172 | "dev": true, 173 | "requires": { 174 | "path-parse": "^1.0.6" 175 | } 176 | }, 177 | "rollup": { 178 | "version": "2.15.0", 179 | "resolved": "https://registry.npmjs.org/rollup/-/rollup-2.15.0.tgz", 180 | "integrity": "sha512-HAk4kyXiV5sdNDnbKWk5zBPnkX/DAgx09Kbp8rRIRDVsTUVN3vnSowR7ZHkV6/lAiE6c2TQ8HtYb72aCPGW4Jw==", 181 | "dev": true, 182 | "requires": { 183 | "fsevents": "~2.1.2" 184 | } 185 | }, 186 | "rollup-plugin-commonjs": { 187 | "version": "10.1.0", 188 | "resolved": "https://registry.npmjs.org/rollup-plugin-commonjs/-/rollup-plugin-commonjs-10.1.0.tgz", 189 | "integrity": "sha512-jlXbjZSQg8EIeAAvepNwhJj++qJWNJw1Cl0YnOqKtP5Djx+fFGkp3WRh+W0ASCaFG5w1jhmzDxgu3SJuVxPF4Q==", 190 | "dev": true, 191 | "requires": { 192 | "estree-walker": "^0.6.1", 193 | "is-reference": "^1.1.2", 194 | "magic-string": "^0.25.2", 195 | "resolve": "^1.11.0", 196 | "rollup-pluginutils": "^2.8.1" 197 | } 198 | }, 199 | "rollup-plugin-execute": { 200 | "version": "1.1.1", 201 | "resolved": "https://registry.npmjs.org/rollup-plugin-execute/-/rollup-plugin-execute-1.1.1.tgz", 202 | "integrity": "sha512-isCNR/VrwlEfWJMwsnmt5TBRod8dW1IjVRxcXCBrxDmVTeA1IXjzeLSS3inFBmRD7KDPlo38KSb2mh5v5BoWgA==", 203 | "dev": true 204 | }, 205 | "rollup-plugin-node-resolve": { 206 | "version": "5.2.0", 207 | "resolved": "https://registry.npmjs.org/rollup-plugin-node-resolve/-/rollup-plugin-node-resolve-5.2.0.tgz", 208 | "integrity": "sha512-jUlyaDXts7TW2CqQ4GaO5VJ4PwwaV8VUGA7+km3n6k6xtOEacf61u0VXwN80phY/evMcaS+9eIeJ9MOyDxt5Zw==", 209 | "dev": true, 210 | "requires": { 211 | "@types/resolve": "0.0.8", 212 | "builtin-modules": "^3.1.0", 213 | "is-module": "^1.0.0", 214 | "resolve": "^1.11.1", 215 | "rollup-pluginutils": "^2.8.1" 216 | } 217 | }, 218 | "rollup-plugin-typescript": { 219 | "version": "1.0.1", 220 | "resolved": "https://registry.npmjs.org/rollup-plugin-typescript/-/rollup-plugin-typescript-1.0.1.tgz", 221 | "integrity": "sha512-rwJDNn9jv/NsKZuyBb/h0jsclP4CJ58qbvZt2Q9zDIGILF2LtdtvCqMOL+Gq9IVq5MTrTlHZNrn8h7VjQgd8tw==", 222 | "dev": true, 223 | "requires": { 224 | "resolve": "^1.10.0", 225 | "rollup-pluginutils": "^2.5.0" 226 | } 227 | }, 228 | "rollup-pluginutils": { 229 | "version": "2.8.2", 230 | "resolved": "https://registry.npmjs.org/rollup-pluginutils/-/rollup-pluginutils-2.8.2.tgz", 231 | "integrity": "sha512-EEp9NhnUkwY8aif6bxgovPHMoMoNr2FulJziTndpt5H9RdwC47GSGuII9XxpSdzVGM0GWrNPHV6ie1LTNJPaLQ==", 232 | "dev": true, 233 | "requires": { 234 | "estree-walker": "^0.6.1" 235 | } 236 | }, 237 | "shebang-command": { 238 | "version": "2.0.0", 239 | "resolved": "https://registry.npmjs.org/shebang-command/-/shebang-command-2.0.0.tgz", 240 | "integrity": "sha512-kHxr2zZpYtdmrN1qDjrrX/Z1rR1kG8Dx+gkpK1G4eXmvXswmcE1hTWBWYUzlraYw1/yZp6YuDY77YtvbN0dmDA==", 241 | "dev": true, 242 | "requires": { 243 | "shebang-regex": "^3.0.0" 244 | } 245 | }, 246 | "shebang-regex": { 247 | "version": "3.0.0", 248 | "resolved": "https://registry.npmjs.org/shebang-regex/-/shebang-regex-3.0.0.tgz", 249 | "integrity": "sha512-7++dFhtcx3353uBaq8DDR4NuxBetBzC7ZQOhmTQInHEd6bSrXdiEyzCvG07Z44UYdLShWUyXt5M/yhz8ekcb1A==", 250 | "dev": true 251 | }, 252 | "sourcemap-codec": { 253 | "version": "1.4.6", 254 | "resolved": "https://registry.npmjs.org/sourcemap-codec/-/sourcemap-codec-1.4.6.tgz", 255 | "integrity": "sha512-1ZooVLYFxC448piVLBbtOxFcXwnymH9oUF8nRd3CuYDVvkRBxRl6pB4Mtas5a4drtL+E8LDgFkQNcgIw6tc8Hg==", 256 | "dev": true 257 | }, 258 | "supports-color": { 259 | "version": "7.1.0", 260 | "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.1.0.tgz", 261 | "integrity": "sha512-oRSIpR8pxT1Wr2FquTNnGet79b3BWljqOuoW/h4oBhxJ/HUbX5nX6JSruTkvXDCFMwDPvsaTTbvMLKZWSy0R5g==", 262 | "dev": true, 263 | "requires": { 264 | "has-flag": "^4.0.0" 265 | } 266 | }, 267 | "tslib": { 268 | "version": "2.0.0", 269 | "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.0.0.tgz", 270 | "integrity": "sha512-lTqkx847PI7xEDYJntxZH89L2/aXInsyF2luSafe/+0fHOMjlBNXdH6th7f70qxLDhul7KZK0zC8V5ZIyHl0/g==", 271 | "dev": true 272 | }, 273 | "typescript": { 274 | "version": "3.9.5", 275 | "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.9.5.tgz", 276 | "integrity": "sha512-hSAifV3k+i6lEoCJ2k6R2Z/rp/H3+8sdmcn5NrS3/3kE7+RyZXm9aqvxWqjEXHAd8b0pShatpcdMTvEdvAJltQ==", 277 | "dev": true 278 | }, 279 | "which": { 280 | "version": "2.0.2", 281 | "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz", 282 | "integrity": "sha512-BLI3Tl1TW3Pvl70l3yq3Y64i+awpwXqsGBYWkkqMtnbXgrMD+yj7rhW0kuEDxzJaYXGjEW5ogapKNMEKNMjibA==", 283 | "dev": true, 284 | "requires": { 285 | "isexe": "^2.0.0" 286 | } 287 | } 288 | } 289 | } 290 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "rollup-plugin-command", 3 | "version": "1.1.3", 4 | "description": "Run commands and call functions when bundles are generated", 5 | "main": "dist/index.cjs.js", 6 | "module": "dist/index.esm.js", 7 | "typings": "typings", 8 | "scripts": { 9 | "build": "cross-env NODE_ENV=production rollup -c && tsc", 10 | "test": "cross-env NODE_ENV=development rollup -c", 11 | "test:bare": "cross-env NODE_ENV=test rollup -c && node dist/index.cjs.js", 12 | "lint": "prettier --write \"./**\"", 13 | "lint:test": "prettier --check \"./**\"", 14 | "preversion": "npm run test && npm run lint:test && npm run build" 15 | }, 16 | "files": [ 17 | "dist", 18 | "typings" 19 | ], 20 | "keywords": [], 21 | "author": "", 22 | "license": "MIT", 23 | "devDependencies": { 24 | "acorn": "^7.2.0", 25 | "chalk": "^4.0.0", 26 | "cross-env": "^7.0.2", 27 | "prettier": "^2.0.5", 28 | "rollup": "^2.15.0", 29 | "rollup-plugin-commonjs": "^10.1.0", 30 | "rollup-plugin-execute": "^1.1.1", 31 | "rollup-plugin-node-resolve": "^5.2.0", 32 | "rollup-plugin-typescript": "^1.0.1", 33 | "tslib": "^2.0.0", 34 | "typescript": "^3.9.5" 35 | }, 36 | "homepage": "https://github.com/Vehmloewff/rollup-plugin-command#readme", 37 | "repository": { 38 | "type": "git", 39 | "url": "https://github.com/Vehmloewff/rollup-plugin-command" 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /rollup.config.js: -------------------------------------------------------------------------------- 1 | import commonjs from 'rollup-plugin-commonjs'; 2 | import resolve from 'rollup-plugin-node-resolve'; 3 | import execute from 'rollup-plugin-execute'; 4 | import pkg from './package.json'; 5 | import typescript from 'rollup-plugin-typescript'; 6 | 7 | const sourcemap = true; 8 | const prod = process.env.NODE_ENV === 'production'; 9 | const test = process.env.NODE_ENV === 'test'; 10 | 11 | const sharedOutputOptions = { 12 | sourcemap, 13 | }; 14 | 15 | const output = [{ file: pkg.main, format: 'cjs', ...sharedOutputOptions }]; 16 | 17 | if (prod) output.push({ file: pkg.module, format: 'esm', ...sharedOutputOptions }); 18 | 19 | export default { 20 | input: prod ? 'src/index.ts' : 'test.ts', 21 | output, 22 | external: ['child_process'], 23 | plugins: [ 24 | resolve({ 25 | preferBuiltins: true, 26 | }), 27 | commonjs(), 28 | !prod && !test && execute(`node ${pkg.main}`), 29 | typescript({ 30 | typescript: require('typescript'), 31 | }), 32 | ], 33 | }; 34 | -------------------------------------------------------------------------------- /src/command.ts: -------------------------------------------------------------------------------- 1 | export type CommandCaller = string | Function; 2 | export type CommandOptions = { exitOnFail?: boolean; once?: boolean; wait?: boolean }; 3 | export type Command = [CommandCaller, CommandOptions]; 4 | 5 | export const defaultCommandOptions: CommandOptions = { 6 | exitOnFail: false, 7 | wait: false, 8 | }; 9 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | import { CommandOptions, CommandCaller, defaultCommandOptions } from './command'; 2 | import run from './run'; 3 | 4 | export default (command: CommandCaller | CommandCaller[], options?: CommandOptions) => { 5 | options = Object.assign({}, defaultCommandOptions, options); 6 | 7 | let called = false; 8 | 9 | return { 10 | name: 'command', 11 | writeBundle: async () => { 12 | if (called && options.once) return; 13 | 14 | called = true; 15 | 16 | let inputs: CommandCaller[] = []; 17 | 18 | if (!Array.isArray(command)) { 19 | inputs[0] = command; 20 | } else { 21 | inputs = command; 22 | } 23 | 24 | for (let i in inputs) { 25 | await run(inputs[i], options); 26 | } 27 | }, 28 | }; 29 | }; 30 | -------------------------------------------------------------------------------- /src/run.ts: -------------------------------------------------------------------------------- 1 | import { CommandCaller, CommandOptions } from './command'; 2 | import { spawn } from 'child_process'; 3 | 4 | export default async (command: CommandCaller, options: CommandOptions) => { 5 | if (typeof command === 'function') return await command(); 6 | if (typeof command !== 'string') 7 | return console.error(`command must be a function or a string. Recieved type ${typeof command}`); 8 | 9 | await new Promise((resolve) => { 10 | if (!options.wait) resolve(); 11 | 12 | spawn(command, { 13 | shell: true, 14 | stdio: 'inherit', 15 | }).on('close', (code) => { 16 | if (options.exitOnFail && code !== 0) { 17 | console.error(`Error: Command exited with ${code}`); 18 | process.exit(1); 19 | } else { 20 | resolve(); 21 | } 22 | }); 23 | }); 24 | }; 25 | -------------------------------------------------------------------------------- /test-colors.js: -------------------------------------------------------------------------------- 1 | const chalk = require('chalk'); 2 | 3 | console.log(chalk.red`hello 3`); 4 | console.log(chalk.red.bold`hello 4`); 5 | console.log(chalk.green.italic(`hello 5`)); 6 | console.log(chalk.blue` hello 6`); 7 | -------------------------------------------------------------------------------- /test.ts: -------------------------------------------------------------------------------- 1 | import command from './src'; 2 | 3 | const fn = command([() => console.log('hello 1'), `echo hello-2`, `node test-colors.js`], { 4 | exitOnFail: true, 5 | wait: false, 6 | }); 7 | 8 | const caller = async () => { 9 | await fn.writeBundle(); 10 | await fn.writeBundle(); 11 | }; 12 | caller(); 13 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "noImplicitAny": true, 4 | "diagnostics": true, 5 | "noImplicitThis": true, 6 | "noEmitOnError": true, 7 | "esModuleInterop": true, 8 | "importHelpers": true, 9 | "declaration": true, 10 | "declarationDir": "typings", 11 | "emitDeclarationOnly": true, 12 | "lib": ["es5", "es6", "dom"] 13 | }, 14 | "target": "ESNext", 15 | "module": "ES6", 16 | "include": ["src", "untyped.d.ts"], 17 | "exclude": ["node_modules"] 18 | } 19 | --------------------------------------------------------------------------------