intreface.cli
Version:
Intreface Dev Tools
199 lines (195 loc) • 9.48 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const command_1 = require("@oclif/command");
const system_1 = require("../@utility/system");
const io_1 = require("../@utility/io");
const chalk_1 = require("chalk");
const inquirer = require("inquirer");
const path = require("path");
const objectPath = require("object-path");
const glob = require("glob");
const cli_ux_1 = require("cli-ux");
const slash = require('slash');
const questions_1 = require("../@utility/questions");
const fs = require("fs");
const notifier = require("node-notifier");
const node_watch_1 = require("node-watch");
const fse = require("fs-extra");
const bitrix_1 = require("../@utility/bitrix");
const lockedProject = bitrix_1.getBitrixLockedProject();
const questionMap = {
gitignore: !lockedProject
? { gitRepositorySelect: questions_1.gitRepositorySelect }
: {},
watch: !lockedProject
? { gitRepositorySelect: questions_1.gitRepositorySelect }
: {},
sync: !lockedProject
? { gitRepositorySelect: questions_1.gitRepositorySelect }
: {}
};
class Project extends command_1.Command {
async run() {
const { args } = this.parse(Project);
const config = objectPath(system_1.readConfig());
switch (args.action) {
case 'sync': {
let questions = Object.values(questionMap[args.action]);
let responses = await inquirer.prompt(questions);
// Set locked project
if (lockedProject !== false) {
responses.gitRepository = lockedProject;
}
let list = system_1.getDirectoryList(config.get('directory.bitrix'));
list = list.filter(item => {
return !io_1.isFileExist(path.join(config.get('directory.bitrix'), item, '.cli.ignore'));
});
list.forEach((project) => {
let targetPath = path.join(config.get('directory.bitrix'), project, 'www/local/projects', responses.gitRepository);
if (io_1.isDirectoryExist(targetPath)) {
targetPath = fse.realpathSync(targetPath);
let destinationPath = path.join(config.get('directory.bitrix'), project, 'www');
fs.readdirSync(targetPath, { withFileTypes: true })
.map(entity => {
if (!['.git', '.gitignore'].includes(entity.name)) {
let innerTargetPath = path.join(targetPath, entity.name);
let innerDestinationPath = path.join(destinationPath, entity.name);
if (entity.isDirectory()) {
if (!io_1.isDirectoryExist(innerDestinationPath)) {
fs.mkdirSync(innerDestinationPath, { recursive: true });
}
}
fse.copySync(innerTargetPath, innerDestinationPath);
}
});
console.log(`Synced to "${destinationPath}".`);
}
});
break;
}
case 'watch': {
let questions = Object.values(questionMap[args.action]);
let responses = await inquirer.prompt(questions);
// Set locked project
if (lockedProject !== false) {
responses.gitRepository = lockedProject;
}
let list = system_1.getDirectoryList(config.get('directory.bitrix'));
list = list.filter(item => {
return !io_1.isFileExist(path.join(config.get('directory.bitrix'), item, '.cli.ignore'));
});
list.forEach((project) => {
let projectPath = path.join(config.get('directory.bitrix'), project, 'www/local/projects', responses.gitRepository);
if (io_1.isDirectoryExist(projectPath)) {
console.log(`Watching "${chalk_1.default.cyanBright.bold(responses.gitRepository)}" in ${projectPath}.`);
node_watch_1.default(projectPath, {
recursive: true,
filter(f) {
return !/node_modules/.test(f) && !/\.idea/.test(f) && !/\.git/.test(f);
}
}, (eventName, targetPath) => {
const rootDirectory = path.join(projectPath, '../../..');
if (path.basename(rootDirectory) != 'www') {
console.log(`${chalk_1.default.red('incorrect root path!' + path.basename(rootDirectory))}`);
return;
}
const destinationPath = path.join(rootDirectory, targetPath.replace(projectPath, ''));
switch (eventName) {
case 'update':
if (io_1.isFile(targetPath)) {
if (!io_1.isDirectoryExist(path.dirname(destinationPath))) {
fs.mkdirSync(path.dirname(destinationPath), { recursive: true });
}
console.log(`${chalk_1.default.green.bold('Copy')} from ${targetPath} to ${destinationPath}`);
fse.copyFileSync(targetPath, destinationPath);
}
break;
case 'remove':
console.log(`${chalk_1.default.red.bold('Remove')} ${destinationPath}`);
fse.removeSync(destinationPath);
break;
}
});
}
});
break;
}
case 'gitignore': {
let gitIgnore = '';
let questions = Object.values(questionMap[args.action]);
let responses = await inquirer.prompt(questions);
// Set locked project
if (lockedProject !== false) {
responses.gitRepository = lockedProject;
}
responses.repositoryPath = slash(path.join(config.get('directory.repository'), responses.gitRepository));
gitIgnore += `### Automatically generated rules @ ${+new Date()}###
\\#build/
.idea
# Logs and databases #
######################
**/*.log
**/*.sql
# OS generated files #
######################
.DS_Store
.DS_Store?
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db
# Project Rules #
######################
`;
// START
cli_ux_1.default.action.start('processing');
const matches = glob.sync(path.join(responses.repositoryPath, '/**/*'), {
dot: true,
ignore: [
path.join(responses.repositoryPath, '/.git*/**'),
path.join(responses.repositoryPath, '/.git*'),
path.join(responses.repositoryPath, '/.idea*/**'),
path.join(responses.repositoryPath, '/.idea*')
],
nosort: true,
});
for (let entity of matches) {
let relativeEntity = entity
.replace(responses.repositoryPath, '')
.replace(/^\/+/, '')
.replace(/\/+$/, '');
if (io_1.isDirectory(entity)) {
gitIgnore += `${relativeEntity}/*
!${relativeEntity}/\n`;
}
else {
gitIgnore += `!${relativeEntity}\n`;
}
}
fs.writeFileSync(`${responses.repositoryPath}/.gitignore`, gitIgnore, { flag: 'w', encoding: 'utf8' });
// END
cli_ux_1.default.action.stop('done');
notifier.notify({
icon: path.join(__dirname, './../@assets/images/logo.png'),
title: 'Intreface CLI',
message: `.gitignore for repository "${responses.gitRepository}" has been generated. 😎`,
});
console.log(`${chalk_1.default.cyan('.gitignore')} for repository "${responses.gitRepository}" has been ${chalk_1.default.green.underline('generated')}.`);
break;
}
default:
this.error(`${chalk_1.default.red('💩')} required argument is ${chalk_1.default.red.underline('Missing')}.`);
break;
}
}
}
exports.default = Project;
Project.description = 'Project helper';
Project.args = [
{ name: 'action' },
];
Project.examples = [
`$ intreface project gitignore`,
`$ intreface project watch`,
`$ intreface project sync`,
];