Skip to content

Instantly share code, notes, and snippets.

@JoshK2
Last active February 24, 2022 10:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoshK2/01218140a64de9ed3706e5ea06fcc0b0 to your computer and use it in GitHub Desktop.
Save JoshK2/01218140a64de9ed3706e5ea06fcc0b0 to your computer and use it in GitHub Desktop.
Import all components from a collection at bit.dev
//IMPORTANT - install thie before: npm i child-process-promise --save-dev
/*
put this in your package.json script:
"import-all": "node import-all.js"
npm run import-all <user>.<collection> amount
*/
const exec = require('child-process-promise').exec;
const args = process.argv;
const remote = args[2];
let amount = args[3];
console.log('remote', remote);
console.log('amount', amount);
exec(`bit list ${remote} -j`, {}).then(function (result) {
if(!amount) amount = 1;
const list = JSON.parse(result.stdout);
const components = list.map(comp => {
return comp.id;
});
const arrayCommands = chunkArrayInGroups(components, amount);
arrayCommands.map(_array => {
const commands = `bit import ${_array.join(' ')}`
console.log(commands);
importComponents(commands);
});
});
function importComponents(command) {
exec(command)
.then(function (result) {
var stdout = result.stdout;
var stderr = result.stderr;
console.log('stdout: ', stdout);
console.log('stderr: ', stderr);
})
.catch(function (err) {
console.error('ERROR: ', err);
});
}
function chunkArrayInGroups(arr, size) {
var newArr=[];
for (var i=0; arr.length>size; i++){
newArr.push(arr.splice(0,size));
}
newArr.push(arr.slice(0));
return newArr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment