UNPKG

@navikt/aksel

Version:

Aksel command line interface. Handles css-imports, codemods and more

188 lines (187 loc) 8.24 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } 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) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.cssImportsCommand = cssImportsCommand; const chalk_1 = __importDefault(require("chalk")); const child_process_1 = require("child_process"); const _mappings_1 = require("@navikt/ds-css/config/_mappings"); const config_1 = require("./config"); const generate_output_1 = require("./generate-output"); const get_directories_1 = require("./get-directories"); const get_version_1 = require("./get-version"); const inquiry_1 = require("./inquiry"); function cssImportsCommand() { return __awaiter(this, void 0, void 0, function* () { const answers = { "config-type": "regular", cdn: "no", version: "0.0.0", autoscan: "no", scandir: "", tailwind: "no", layers: "no", imports: null, output: "print-clipboard", }; yield (0, inquiry_1.inquiry)(answers, [ { type: "select", name: "config-type", message: "Import variants:", initial: 0, choices: [ { message: "Regular import (recommended)", name: "regular" }, { message: "Partial control (global)", name: "easy" }, { message: "Full control (global + components)", name: "advanced" }, ], footer() { return chalk_1.default.grey(`${chalk_1.default.cyan(`\n Documentation:`)}\n Regular: https://aksel.nav.no/grunnleggende/kode/css-import Partial: https://aksel.nav.no/grunnleggende/kode/css-import#h64650b1a4ad6 Full: https://aksel.nav.no/grunnleggende/kode/css-import#h4037598416ef\n`); }, }, { type: "select", name: "cdn", message: "Import format", initial: 0, choices: [ { message: "Static import (default)", name: "no" }, { message: "CDN import (not recommended)", name: "yes" }, ], }, ]); if ((answers === null || answers === void 0 ? void 0 : answers.cdn) === "no") { yield (0, inquiry_1.inquiry)(answers, [ { type: "select", name: "tailwind", message: "Add tailwind support?", initial: 0, choices: [ { message: "No", name: "no" }, { message: "Yes", name: "yes" }, ], }, { type: "select", name: "layers", message: "Add styling to custom @layer rule?", initial: 0, choices: [ { message: "No", name: "no" }, { message: "Yes", name: "yes" }, ], }, ]); } else { let versions = (yield (0, get_version_1.getAllVersions)()).filter((x) => !x.includes("-")); const index = versions.findIndex((x) => x.startsWith("2.9.0")); versions = versions.slice(index).reverse(); yield (0, inquiry_1.inquiry)(answers, [ { type: "autocomplete", name: "version", message: `@navikt/ds-css version from CDN:`, limit: 6, initial: 0, choices: versions, footer() { return chalk_1.default.grey('Remember to match version with @navikt/ds-react!\nNote: CDN was introduced in v2.9.0, older versions not available.\nUse "static" import instead.'); }, }, ]); } if (answers["config-type"] === "regular") { yield (0, generate_output_1.generateImportOutput)(answers); return; } answers["config-type"] === "advanced" && (yield (0, inquiry_1.inquiry)(answers, [ { type: "select", name: "autoscan", message: "Scan current directory for '@navikt/ds-react' components?", initial: 0, choices: [ { message: "No", name: "no" }, { message: "Yes", name: "yes" }, ], }, ])); answers.autoscan === "yes" && (yield (0, inquiry_1.inquiry)(answers, [ { type: "autocomplete", name: "scandir", message: `Directory to scan`, limit: 6, initial: 0, choices: (0, get_directories_1.getDirectories)(), footer() { return chalk_1.default.grey("filtered out: node_moduels, dist, build, lib, .* (dotfiles)"); }, }, ])); let foundComponents = []; if (answers.autoscan === "yes") { foundComponents = yield new Promise((resolve) => { (0, child_process_1.exec)(`node ${__dirname}/scan-code.js ${answers.scandir}`, (_, stdout) => { resolve(stdout ? JSON.parse(stdout.trim().split("\n").slice(1).join("")) : []); }); }); } yield (0, inquiry_1.inquiry)(answers, [ { type: "multiselect", name: "imports", message: "Imports", initial: [ ..._mappings_1.StyleMappings.baseline.map((x) => x.main.replace(".css", "")), ..._mappings_1.StyleMappings.components .filter((x) => foundComponents.includes(x.component)) .map((x) => `${config_1.ComponentPrefix}${x.component}`), ], choices: [ { message: "Default-imports", name: "default", choices: [ ..._mappings_1.StyleMappings.baseline.map((x) => ({ message: `${x.main.replace(".css", "")}${x.optional ? "" : " (required)"}`, name: x.main.replace(".css", ""), })), ], }, ...(answers["config-type"] === "advanced" ? [ { message: "Components", name: "components", choices: [ ..._mappings_1.StyleMappings.components.map((x) => ({ message: x.component, name: `${config_1.ComponentPrefix}${x.component}`, })), ], }, ] : []), ], }, ]); yield (0, generate_output_1.generateImportOutput)(answers); }); }