I created a node script which checks if my project contains lock file or not. If it doesn't then I want to abort my npm build. Any idea how to do that?
lock-check.js
const path = require('path');
const fs = require("fs");
const lockFiles = ["package-lock.json", "npm-shrinkwrap.json", "yarn.lock"];
let exists = 0;
function checkIfExists() {
lockFiles.forEach(
(lf) => {
if (fs.existsSync(lf)) {
exists++;
}
});
return exists > 0;
}
package.json
...
"scripts": {
"prestart": "node ./lock-check.js" // Abort the task
"start": "webpack-dev-server --config config/webpack.dev.js --hot --inline"
}
...
To abort the build process you just have to call process.exit(1),
Here I have used 1 but you can use any non-zero exit code to tell it wasn't a successful build as 0 means successful.
You can read more on official nodejs docs
I have read the document of build library in VUE-CLI3.0.
My directory:
--src
--components
--componentA.vue
--componentB.vue
....
--componentZ.vue
--build
--libs.js
I want to run one command with my one entry "libs.js" (Maybe there is a loop to create multiple entries in libs.js) to bundle my components separately. The destination folder maybe like the following:
--dist
--componentA.css
--componentA.command.js
--componentA.umd.js
--componentA.umd.min.js
...
--componentZ.css
--componentZ.command.js
--componentZ.umd.js
--componentZ.umd.min.js
Can anyone give me some suggetions?
I add a script file. In which, I get the list of components and using 'child_process' to execute each command.
The following is an example:
lib.js
const { execSync } = require('child_process')
const glob = require('glob')
// console font color
const chalk = require('chalk')
// loading
const ora = require('ora')
// 获取所有的moduleList
const components = glob.sync('./src/components/*.vue')
// const buildFile = path.join(__dirname, 'build.js')
// const webpack = require('vuec')
const spinner = ora('Packaging the components...\n').start()
setTimeout(() => {
spinner.stop()
}, 2000)
for (const component of components) {
// const file = path.join(__dirname, module);
const name = component.substring(component.lastIndexOf('/') + 1).slice(0, -4)
const cmd = `vue build -t lib -n ${name} ${component} -d lib/components/${name}`
execSync(cmd)
console.log(chalk.blue(`Component ${name} is packaged.`))
}
console.log(`[${new Date()}]` + chalk.green('Compeleted !'))
What's more, add a script command in package.json:
"build-all": "node ./src/build/lib.js"
You just enter npm run build-all. That's all~
I have this in my package.json
script: {
"myscript": "babel-node script.js"
}
In the terminal I do npm run myscript my-param. I'm able to get
my-param in script.js, I have this in script.js
const argv = require('yargs').argv
const { _ : [ my_param ] } = argv
console.log(my_param)
But if I chain my npm script like so
script: {
"myscript": "babel-node script.js && node_modules/karma/bin/karma start karma.config.js"
}
Then I can't pass anything using npm run myscript.js script.js from the terminal anymore? I don't see my-param in the console.log
I'm trying to get my js script to pull the TRAVIS_PULL_REQUEST_BRANCH value but it comes up undefined when my js script runs. Not sure what I'm doing wrong here:
deploy-pull-request.js
const chalk = require('chalk'),
_ = require('lodash'),
green = chalk.green,
info = chalk.yellow,
Deploy = require('./deployApi')
// require('dotenv').config()
// const { env } = process
let options = {
awsKey: process.env.AWS_ACCESS_KEY_ID,
awsSecret: process.env.AWS_SECRET_ACCESS_KEY,
localBuildFolder: 'build',
domain: 'admin-'
}
const branch = process.env.FAKE_PULL_REQUEST_BRANCH || process.env.TRAVIS_PULL_REQUEST_BRANCH
.. rest of the code...
travis.yml
language: node_js
node_js:
- 8
cache:
yarn: true
directories:
- node_modules
deploy:
provider: s3
access_key_id: $AWS_ACCESS_KEY_ID
secret_access_key: $AWS_SECRET_ACCESS_KEY
before_script:
- pip install --user awscli
- yarn run build
- yarn run test
script:
- babel-node ./src/client/deploy/deploy-pull-request.js
Is there a way to specify a gulp task depending on the NODE_ENV that is set?
For example in my package.json file, I have something like:
"scripts": {
"start": "gulp"
}
And I have multiple gulp tasks
gulp.task('development', function () {
// run dev related tasks like watch
});
gulp.task('production', function () {
// run prod related tasks
});
If I set NODE_ENV=production npm start, can I specify to only run gulp production? Or is there a better way to do this?
Using a single ternary in your default gulp task, you can have something like:
gulp.task('default',
[process.env.NODE_ENV === 'production' ? 'production' : 'development']
);
You will then be able to keep the single gulp command in your package.json and using this like you said:
NODE_ENV=production npm start
Any other value of your NODE_ENV variable will launch the development task.
You could of course do an advanced usage using an object allowing for multiple tasks and avoiding if trees hell:
var tasks = {
development: 'development',
production: ['git', 'build', 'publish'],
preprod: ['build:preprod', 'publish:preprod'],
...
}
gulp.task('default', tasks[process.env.NODE_ENV] || 'fallback')
Keep in mind that when giving an array of tasks, they will be run in parallel.
Have your first gulp task run other gulp tasks based on the process.env.NODE_ENV value.
gulp.task('launcher', function(){
switch (process.env.NODE_ENV){
case 'development':
// Run dev tasks from here
break;
case 'production':
// Run prod tasks
break;
}
});
The other simple way could be
gulp.task('set-dev-env', function () {
return process.env.NODE_ENV = 'development';
});
gulp.task('set-prod-env', function () {
return process.env.NODE_ENV = 'production';
});
gulp.task('development', ['set-dev-env'], function () {
// your code
});
gulp.task('production', ['set-prod-env'], function () {
// your code
});
Run gulp production or gulp development.
You can also use gulp-mode plugin.
Usage:
var gulp = require('gulp');
var mode = require('gulp-mode')();
var uglify = require('gulp-uglify');
gulp.task('default', function() {
gulp.src('src/*.js')
.pipe(mode.production(uglify()))
.pipe(gulp.dest('dist'));
});
OR
var isProduction = mode.production();
if (isProduction) {
console.log("Production mode");
}
Start build as:
gulp build --production
For "npm run-script" use-cases:
package.json :-
"scripts": {
"devbld": "gulp build --development",
"prodbld": "gulp build --production",
}
$ npm run devbld
$ npm run prodbld
if (process.env.NODE_ENV === "production")
// whatever