From 50c01bbbdd19cc2bd78431deca30df82929ba991 Mon Sep 17 00:00:00 2001 From: Valera Rozuvan Date: Mon, 26 Oct 2020 03:28:01 +0200 Subject: [PATCH] Added basic file/folder checks. --- bin/main.js | 121 ++++++++++++++++++++++++++++++++++++++++++-- bin/main.js.map | 2 +- npm-shrinkwrap.json | 51 +++++++++++++++++-- package.json | 3 +- src/main.ts | 114 ++++++++++++++++++++++++++++++++++++++--- 5 files changed, 274 insertions(+), 17 deletions(-) diff --git a/bin/main.js b/bin/main.js index e9704a3..617f95a 100644 --- a/bin/main.js +++ b/bin/main.js @@ -1,16 +1,127 @@ #!/usr/bin/env node "use strict"; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; Object.defineProperty(exports, "__esModule", { value: true }); -const app_version_1 = require("./app-version"); const mdlinkc_1 = require("mdlinkc"); +const chalk = require("chalk"); +const path = require("path"); +const app_version_1 = require("./app-version"); function greeter(msg) { return msg; } -const helloMsg = `mdlinkc v${app_version_1.AppVersion.version}`; +const helloMsg = `mdlinkc-cli v${app_version_1.AppVersion.version}`; console.log(greeter(helloMsg)); console.log(''); -mdlinkc_1.Mdlinkc.getLinks().forEach((link, id) => { - console.log(`[link ${id}]: ${link}`); -}); +const passSymbol = '\u2714'; +const failSymbol = '\u2716'; +function testDir(dirName) { + return __awaiter(this, void 0, void 0, function* () { + let status = yield mdlinkc_1.Mdlinkc.checkIfDirExists(dirName); + if (status) { + console.log(`[${chalk.green(passSymbol)}] ${chalk.bold(dirName)} directory exists.`); + return 1; + } + else { + console.log(`[${chalk.red(failSymbol)}] ${chalk.bold(dirName)} directory does NOT exist.`); + return 0; + } + }); +} +function checkRequiredDirs() { + return __awaiter(this, void 0, void 0, function* () { + let testDirCounter = 0; + const dirsToTest = ['templates', 'contents', 'configs', 'scripts']; + for (let c1 = 0; c1 < dirsToTest.length; c1 += 1) { + const dir = dirsToTest[c1]; + testDirCounter += yield testDir(dir); + } + if (testDirCounter !== 4) { + console.log(''); + console.log('Some directories are missing. Exiting ...'); + process.exit(1); + } + }); +} +function loadConfigFile(configName) { + return __awaiter(this, void 0, void 0, function* () { + const shortPath = `./configs/${configName}.js`; + const fullPath = path.join(process.cwd(), shortPath); + let config; + try { + config = require(fullPath); + console.log(`[${chalk.green(passSymbol)}] ${chalk.bold(shortPath)} config file was loaded.`); + return config; + } + catch (err) { + console.log(`[${chalk.red(failSymbol)}] ${chalk.bold(shortPath)} config file could NOT be loaded.`); + return null; + } + }); +} +function loadAllConfigs(configs) { + return __awaiter(this, void 0, void 0, function* () { + let configCounter = 0; + const configsToLoad = ['pages', 'variables', 'meta']; + for (let c1 = 0; c1 < configsToLoad.length; c1 += 1) { + const configName = configsToLoad[c1]; + configs[configName] = yield loadConfigFile(configName); + if (configs[configName] !== null) { + configCounter += 1; + } + } + if (configCounter !== 3) { + console.log(''); + console.log('Some configs are missing. Exiting ...'); + process.exit(1); + } + }); +} +function printNumThreads(configs) { + return __awaiter(this, void 0, void 0, function* () { + console.log('configs.meta = ', configs.meta); + console.log('configs.meta.threads = ', configs.meta.threads); + }); +} +function printFirstPage(configs) { + return __awaiter(this, void 0, void 0, function* () { + console.log('configs.pages = ', configs.pages); + console.log('configs.pages[0] = ', configs.pages[0]); + }); +} +function printAuthorVariable(configs) { + return __awaiter(this, void 0, void 0, function* () { + console.log('configs.variables = ', configs.variables); + console.log('configs.variables.AUTHOR = ', configs.variables.AUTHOR); + }); +} +function run(configs) { + return __awaiter(this, void 0, void 0, function* () { + console.log('[DIRS]'); + yield checkRequiredDirs(); + console.log(''); + console.log('[CONFIGS]'); + yield loadAllConfigs(configs); + console.log(''); + yield printNumThreads(configs); + console.log(''); + yield printFirstPage(configs); + console.log(''); + yield printAuthorVariable(configs); + console.log(''); + }); +} +const configs = { + pages: null, + variables: null, + meta: null +}; +run(configs); //# sourceMappingURL=main.js.map \ No newline at end of file diff --git a/bin/main.js.map b/bin/main.js.map index 448d582..962f72e 100644 --- a/bin/main.js.map +++ b/bin/main.js.map @@ -1 +1 @@ -{"version":3,"file":"main.js","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":";;AAAA,+CAA2C;AAC3C,qCAAkC;AAElC,SAAS,OAAO,CAAC,GAAW;IAC1B,OAAO,GAAG,CAAC;AACb,CAAC;AAED,MAAM,QAAQ,GAAG,YAAY,wBAAU,CAAC,OAAO,EAAE,CAAC;AAElD,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC;AAC/B,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;AAEhB,iBAAO,CAAC,QAAQ,EAAE,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE,EAAE,EAAE;IACtC,OAAO,CAAC,GAAG,CAAC,SAAS,EAAE,MAAM,IAAI,EAAE,CAAC,CAAC;AACvC,CAAC,CAAC,CAAC"} \ No newline at end of file +{"version":3,"file":"main.js","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,qCAAkC;AAClC,+BAA8B;AAC9B,6BAA4B;AAE5B,+CAA2C;AAE3C,SAAS,OAAO,CAAC,GAAW;IAC1B,OAAO,GAAG,CAAC;AACb,CAAC;AAED,MAAM,QAAQ,GAAG,gBAAgB,wBAAU,CAAC,OAAO,EAAE,CAAC;AACtD,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC;AAC/B,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;AAEhB,MAAM,UAAU,GAAG,QAAQ,CAAA;AAC3B,MAAM,UAAU,GAAG,QAAQ,CAAA;AAE3B,SAAe,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C,CAAA"} \ No newline at end of file diff --git a/npm-shrinkwrap.json b/npm-shrinkwrap.json index 7eec680..9f6f1b3 100644 --- a/npm-shrinkwrap.json +++ b/npm-shrinkwrap.json @@ -1,6 +1,6 @@ { "name": "mdlinkc-cli", - "version": "0.0.2", + "version": "0.0.4", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -10,10 +10,45 @@ "integrity": "sha512-fh+pAqt4xRzPfqA6eh3Z2y6fyZavRIumvjhaCL753+TVkGKGhpPeyrJG2JftD0T9q4GF00KjefsQ+PQNDdWQaQ==", "dev": true }, + "ansi-styles": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz", + "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==", + "requires": { + "color-convert": "^2.0.1" + } + }, + "chalk": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.0.tgz", + "integrity": "sha512-qwx12AxXe2Q5xQ43Ac//I6v5aXTipYrSESdOgzrN+9XjgEpyjpKuvSGaN4qE93f7TQTlerQQ8S+EQ0EyDoVL1A==", + "requires": { + "ansi-styles": "^4.1.0", + "supports-color": "^7.1.0" + } + }, + "color-convert": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", + "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==", + "requires": { + "color-name": "~1.1.4" + } + }, + "color-name": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", + "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==" + }, + "has-flag": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", + "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==" + }, "mdlinkc": { - "version": "0.0.3", - "resolved": "https://registry.npmjs.org/mdlinkc/-/mdlinkc-0.0.3.tgz", - "integrity": "sha512-7oj43ZDDRNlVjFJo7Rh6nji/dXve7Bdzwy9qJ20ALjjDNln1pSl8dvmrv4n2/cXAtdUqN7IVuTd0wUIjjC3oRw==", + "version": "0.0.4", + "resolved": "https://registry.npmjs.org/mdlinkc/-/mdlinkc-0.0.4.tgz", + "integrity": "sha512-mOOvY8rVj3Mzll+puBdkih3GboP9uU7mmdjeAltTFfw3CGxnxfcrZBiXOEGrMViU5WQ98shfUPRwcH/GbWCv+w==", "dependencies": { "@types/node": { "version": "10.12.18", @@ -42,6 +77,14 @@ "tmp": "0.0.31" } }, + "supports-color": { + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz", + "integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==", + "requires": { + "has-flag": "^4.0.0" + } + }, "tmp": { "version": "0.0.31", "resolved": "https://registry.npmjs.org/tmp/-/tmp-0.0.31.tgz", diff --git a/package.json b/package.json index 1153687..f145b2b 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,8 @@ "clean": "rm -rf ./bin/*" }, "dependencies": { - "mdlinkc": "0.0.3" + "chalk": "^4.1.0", + "mdlinkc": "0.0.4" }, "devDependencies": { "@types/node": "10.12.18", diff --git a/src/main.ts b/src/main.ts index 7b39036..7072e25 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,15 +1,117 @@ -import { AppVersion } from './app-version'; import { Mdlinkc } from 'mdlinkc'; +import * as chalk from 'chalk' +import * as path from 'path' + +import { AppVersion } from './app-version'; function greeter(msg: string) { return msg; } -const helloMsg = `mdlinkc v${AppVersion.version}`; - +const helloMsg = `mdlinkc-cli v${AppVersion.version}`; console.log(greeter(helloMsg)); console.log(''); -Mdlinkc.getLinks().forEach((link, id) => { - console.log(`[link ${id}]: ${link}`); -}); +const passSymbol = '\u2714' +const failSymbol = '\u2716' + +async function testDir(dirName) { + let status = await Mdlinkc.checkIfDirExists(dirName) + + if (status) { + console.log(`[${chalk.green(passSymbol)}] ${chalk.bold(dirName)} directory exists.`) + return 1 + } else { + console.log(`[${chalk.red(failSymbol)}] ${chalk.bold(dirName)} directory does NOT exist.`) + return 0 + } +} + +async function checkRequiredDirs() { + let testDirCounter = 0 + const dirsToTest = ['templates', 'contents', 'configs', 'scripts'] + for (let c1 = 0; c1 < dirsToTest.length; c1 += 1) { + const dir = dirsToTest[c1] + testDirCounter += await testDir(dir) + } + if (testDirCounter !== 4) { + console.log('') + console.log('Some directories are missing. Exiting ...') + process.exit(1) + } +} + +async function loadConfigFile(configName) { + const shortPath = `./configs/${configName}.js` + const fullPath = path.join(process.cwd(), shortPath) + + let config + try { + config = require(fullPath) + console.log(`[${chalk.green(passSymbol)}] ${chalk.bold(shortPath)} config file was loaded.`) + return config + } catch (err) { + console.log(`[${chalk.red(failSymbol)}] ${chalk.bold(shortPath)} config file could NOT be loaded.`) + return null + } +} + +async function loadAllConfigs(configs) { + let configCounter = 0 + const configsToLoad = ['pages', 'variables', 'meta'] + for (let c1 = 0; c1 < configsToLoad.length; c1 += 1) { + const configName = configsToLoad[c1] + configs[configName] = await loadConfigFile(configName) + + if (configs[configName] !== null) { + configCounter += 1 + } + } + if (configCounter !== 3) { + console.log('') + console.log('Some configs are missing. Exiting ...') + process.exit(1) + } +} + +async function printNumThreads(configs) { + console.log('configs.meta = ', configs.meta) + console.log('configs.meta.threads = ', configs.meta.threads) +} + +async function printFirstPage(configs) { + console.log('configs.pages = ', configs.pages) + console.log('configs.pages[0] = ', configs.pages[0]) +} + +async function printAuthorVariable(configs) { + console.log('configs.variables = ', configs.variables) + console.log('configs.variables.AUTHOR = ', configs.variables.AUTHOR) +} + +async function run(configs) { + console.log('[DIRS]') + await checkRequiredDirs() + console.log('') + + console.log('[CONFIGS]') + await loadAllConfigs(configs) + console.log('') + + await printNumThreads(configs) + console.log('') + + await printFirstPage(configs) + console.log('') + + await printAuthorVariable(configs) + console.log('') +} + +const configs = { + pages: null, + variables: null, + meta: null +} + +run(configs)