wishthis/node_modules/fomantic-ui/tasks/admin/components/init.js

164 lines
4.8 KiB
JavaScript
Raw Normal View History

2022-01-13 13:00:31 +00:00
/*******************************
Init Repos
*******************************/
/*
This task pulls the latest version of each component from GitHub
2022-06-08 10:36:39 +00:00
* Creates new repo if doesn't exist (locally & GitHub)
* Adds remote it doesn't exists
2022-01-13 13:00:31 +00:00
* Pulls latest changes from repo
*/
2023-08-17 09:47:40 +00:00
const
gulp = require('gulp'),
// node dependencies
console = require('better-console'),
del = require('del'),
fs = require('fs'),
path = require('path'),
git = require('gulp-git'),
mkdirp = require('mkdirp'),
// admin files
release = require('../../config/admin/release'),
project = require('../../config/project/release'),
// oAuth configuration for GitHub
oAuth = fs.existsSync(path.join(__dirname, '/../../config/admin/oauth.js'))
? require('../../config/admin/oauth.js') // eslint-disable-line import/extensions
: false,
// shorthand
version = project.version
2022-01-13 13:00:31 +00:00
;
2023-08-17 09:47:40 +00:00
module.exports = function (callback) {
const github = require('../../config/admin/github'); // eslint-disable-line global-require
2022-01-13 13:00:31 +00:00
2023-08-17 09:47:40 +00:00
let
index = -1,
total = release.components.length,
timer,
stream,
stepRepo
;
2022-01-13 13:00:31 +00:00
2023-08-17 09:47:40 +00:00
if (!oAuth) {
console.error('Must add oauth token for GitHub in tasks/config/admin/oauth.js');
2022-01-13 13:00:31 +00:00
2023-08-17 09:47:40 +00:00
return;
2022-01-13 13:00:31 +00:00
}
2023-08-17 09:47:40 +00:00
// Do Git commands synchronously per component, to avoid issues
stepRepo = function () {
index += 1;
2022-01-13 13:00:31 +00:00
2023-08-17 09:47:40 +00:00
if (index >= total) {
callback();
2022-01-13 13:00:31 +00:00
2023-08-17 09:47:40 +00:00
return;
}
2022-01-13 13:00:31 +00:00
2023-08-17 09:47:40 +00:00
let
component = release.components[index],
outputDirectory = path.resolve(release.outputRoot + component),
capitalizedComponent = component.charAt(0).toUpperCase() + component.slice(1),
repoName = release.componentRepoRoot + capitalizedComponent,
2022-01-13 13:00:31 +00:00
2023-08-17 09:47:40 +00:00
gitOptions = { cwd: outputDirectory },
pullOptions = { args: '-q', cwd: outputDirectory, quiet: true },
resetOptions = { args: '-q --hard', cwd: outputDirectory, quiet: true },
2022-01-13 13:00:31 +00:00
2023-08-17 09:47:40 +00:00
gitURL = 'git@github.com:' + release.org + '/' + repoName + '.git',
repoURL = 'https://github.com/' + release.org + '/' + repoName + '/',
localRepoSetup = fs.existsSync(path.join(outputDirectory, '.git'))
;
2022-01-13 13:00:31 +00:00
2023-08-17 09:47:40 +00:00
console.log('Processing repository: ' + outputDirectory);
2022-01-13 13:00:31 +00:00
2023-08-17 09:47:40 +00:00
// create folder if doesn't exist
if (!fs.existsSync(outputDirectory)) {
mkdirp.sync(outputDirectory);
2022-01-13 13:00:31 +00:00
}
2023-08-17 09:47:40 +00:00
// clean folder
if (release.outputRoot.startsWith('../repos')) {
console.info('Cleaning dir', outputDirectory);
del.sync([outputDirectory + '**/*'], { silent: true, force: true });
}
2022-01-13 13:00:31 +00:00
2023-08-17 09:47:40 +00:00
// set-up local repo
function setupRepo() {
if (localRepoSetup) {
addRemote();
} else {
initRepo();
}
}
2022-01-13 13:00:31 +00:00
2023-08-17 09:47:40 +00:00
function initRepo() {
console.info('Initializing repository for ' + component);
git.init(gitOptions, function (error) {
if (error) {
console.error('Error initializing repo', error);
}
addRemote();
});
}
2022-01-13 13:00:31 +00:00
2023-08-17 09:47:40 +00:00
function createRepo() {
console.info('Creating GitHub repo ' + repoURL);
github.repos.createFromOrg({
org: release.org,
name: repoName,
homepage: release.homepage,
}, function () {
setupRepo();
});
}
2022-01-13 13:00:31 +00:00
2023-08-17 09:47:40 +00:00
function addRemote() {
console.info('Adding remote origin as ' + gitURL);
git.addRemote('origin', gitURL, gitOptions, function () {
pullFiles();
});
}
2022-01-13 13:00:31 +00:00
2023-08-17 09:47:40 +00:00
function pullFiles() {
console.info('Pulling ' + component + ' files');
git.pull('origin', 'master', pullOptions, function (error) {
resetFiles();
});
}
2022-01-13 13:00:31 +00:00
2023-08-17 09:47:40 +00:00
function resetFiles() {
console.info('Resetting files to head');
git.reset('HEAD', resetOptions, function (error) {
nextRepo();
});
}
2022-01-13 13:00:31 +00:00
2023-08-17 09:47:40 +00:00
function nextRepo() { // eslint-disable-line unicorn/consistent-function-scoping
// console.log('Sleeping for 1 second...');
// avoid rate throttling
global.clearTimeout(timer);
timer = global.setTimeout(function () {
stepRepo();
}, 0);
}
2022-01-13 13:00:31 +00:00
2023-08-17 09:47:40 +00:00
if (localRepoSetup) {
pullFiles();
} else {
setupRepo();
// createRepo() only use to create remote repo (easier to do manually)
}
};
2022-01-13 13:00:31 +00:00
2023-08-17 09:47:40 +00:00
stepRepo();
2022-01-13 13:00:31 +00:00
};