112 lines
4.3 KiB
JavaScript
112 lines
4.3 KiB
JavaScript
#!/usr/bin/env node
|
||
|
||
const fs = require('fs');
|
||
const path = require('path');
|
||
const AdmZip = require('adm-zip');
|
||
const minimist = require('minimist');
|
||
|
||
const helpMessage = `Список доступных команд docidoci:
|
||
help — вывод списка доступных команд
|
||
settings — создаёт файл в котором нужно указать мета-теги для сайта
|
||
init — создание структуры проекта на основе указанных настроек
|
||
`
|
||
|
||
function rebrendingData() {
|
||
const settingsPath = path.join(process.cwd(), 'docidociSettings.json')
|
||
const indexHtmlPath = path.join(process.cwd(), 'index.html')
|
||
const settings = JSON.parse(fs.readFileSync(settingsPath))
|
||
|
||
data = String(fs.readFileSync('./index.html'))
|
||
htmlSample = data.replace('{projectName}', settings.projectName)
|
||
htmlSample = htmlSample.replace('{repo}', settings.repo)
|
||
htmlSample = htmlSample.replace(/{themeColor}/g, settings.themeColor)
|
||
htmlSample = htmlSample.replace('{preloaderText}', settings.preloaderText)
|
||
htmlSample = htmlSample.replace(/{title}/g, settings.seo.title)
|
||
htmlSample = htmlSample.replace(/{description}/g, settings.seo.description)
|
||
htmlSample = htmlSample.replace('{keywords}', settings.seo.keywords)
|
||
htmlSample = htmlSample.replace('{image}', settings.seo.image)
|
||
htmlSample = htmlSample.replace('{url}', settings.seo.url)
|
||
|
||
fs.writeFileSync(indexHtmlPath, htmlSample)
|
||
}
|
||
|
||
function castomCopyFile(from, to) {
|
||
const originalFile = path.join(__dirname, from);
|
||
const copyFile = path.join(process.cwd(), to);
|
||
fs.copyFileSync(originalFile, copyFile);
|
||
}
|
||
|
||
|
||
function commandInit() {
|
||
const archivePath = path.join(__dirname, './files/files.zip');
|
||
const targetPath = process.cwd();
|
||
const settingsPath = path.join(process.cwd(), 'docidociSettings.json')
|
||
|
||
// Проверяем наличие архива
|
||
if (!fs.existsSync(archivePath)) {
|
||
console.log('Архив со структурой проекта не найден!');
|
||
process.exit(1)
|
||
}
|
||
|
||
// Проверяем, существует ли уже проект
|
||
let indexHtmlPath = path.join(process.cwd(), './index.html')
|
||
if (fs.existsSync(indexHtmlPath) && !argv.y) {
|
||
console.log(`Ошибка инициализации!
|
||
|
||
В папке "${targetPath}" уже существует проект!
|
||
Если хочешь его реинициализировать, то введи команду "docidoci init -y"`);
|
||
process.exit(1)
|
||
}
|
||
|
||
// Разархивировываем архив
|
||
const zip = new AdmZip(archivePath);
|
||
zip.extractAllTo(targetPath, true);
|
||
|
||
// Переносим html шаблон
|
||
if (fs.existsSync(settingsPath)) {
|
||
castomCopyFile('./files/index.html', 'index.html')
|
||
rebrendingData()
|
||
} else {
|
||
castomCopyFile('./files/indexWithoutSettings.html', 'index.html')
|
||
}
|
||
|
||
console.log(`Готово, проект инициализирован)
|
||
|
||
Проект находится в папке ${targetPath}
|
||
Запустить проект docsify можно командой "docsify serve"`);
|
||
process.exit()
|
||
};
|
||
|
||
function commandSettings() {
|
||
castomCopyFile('./files/docidociSettings.json', 'docidociSettings.json')
|
||
console.log(`Файл настроек скопирован!
|
||
|
||
Заполни поля своими данными или сделай их пустыми.
|
||
После выполни команду "docidoci init"`);
|
||
}
|
||
|
||
|
||
// Обработка аргументов из командной строки
|
||
const argv = minimist(process.argv.slice(2));
|
||
if (argv._.length == 0) {
|
||
console.log(helpMessage);
|
||
return
|
||
} else if (argv._.length > 1) {
|
||
console.log('За раз можно указывать только одну команду!!!\n');
|
||
console.log(helpMessage);
|
||
return
|
||
}
|
||
|
||
|
||
// Обработка команд
|
||
const command = argv._[0] // основная команда
|
||
if (command === 'init') {
|
||
commandInit();
|
||
} else if (command == 'help') {
|
||
console.log(helpMessage)
|
||
} else if (command == 'settings') {
|
||
commandSettings()
|
||
} else {
|
||
console.log(`Неизвестная команда "${command}". Введи "docidoci help", чтобы увидеть список доступных команд`);
|
||
}
|