@navikt/aksel
Version:
Aksel command line interface. Handles css-imports, codemods and more
126 lines (125 loc) • 5.13 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getStatus = getStatus;
const cli_progress_1 = __importDefault(require("cli-progress"));
const fs_1 = __importDefault(require("fs"));
const TokenStatus_1 = require("../config/TokenStatus");
const darkside_tokens_1 = require("../config/darkside.tokens");
const legacy_component_tokens_1 = require("../config/legacy-component.tokens");
const legacy_tokens_1 = require("../config/legacy.tokens");
const token_regex_1 = require("../config/token-regex");
const StatusStore = new TokenStatus_1.TokenStatus();
/**
* Get the status of the tokens in the files
*/
function getStatus(files, action = "print") {
const progressBar = new cli_progress_1.default.SingleBar({
clearOnComplete: true,
hideCursor: true,
format: "{bar} | {value}/{total} | {fileName}",
}, cli_progress_1.default.Presets.shades_classic);
if (action === "print") {
progressBar.start(files.length, 0);
}
StatusStore.initStatus();
files.forEach((fileName, index) => {
const fileSrc = fs_1.default.readFileSync(fileName, "utf8");
/**
* We first parse trough all legacy tokens (--a-) prefixed tokens
*/
for (const [legacyToken, config] of Object.entries(legacy_tokens_1.legacyTokenConfig)) {
if (!(0, token_regex_1.getTokenRegex)(legacyToken, "css").test(fileSrc)) {
continue;
}
for (const [regexKey, regex] of Object.entries(config.regexes)) {
if (!regex) {
continue;
}
let match = regex.exec(fileSrc);
while (match) {
const { row, column } = getWordPositionInFile(fileSrc, match.index);
StatusStore.add({
isLegacy: true,
comment: config.comment,
type: regexKey,
columnNumber: column,
lineNumber: row,
canAutoMigrate: regexKey === "tailwind"
? !!config.twNew
: config.replacement.length > 0,
fileName,
name: match[0],
});
match = regex.exec(fileSrc);
}
}
}
const legacyRegex = new RegExp(`(${legacy_component_tokens_1.legacyComponentTokenList.map((t) => `${t}:`).join("|")})`, "gm");
let legacyMatch = legacyRegex.exec(fileSrc);
while (legacyMatch !== null) {
const { row, column } = getWordPositionInFile(fileSrc, legacyMatch.index);
StatusStore.add({
isLegacy: true,
type: "component",
columnNumber: column,
lineNumber: row,
canAutoMigrate: false,
fileName,
name: legacyMatch[0],
});
legacyMatch = legacyRegex.exec(fileSrc);
}
for (const [newTokenName, config] of Object.entries(darkside_tokens_1.darksideTokenConfig)) {
if (!(0, token_regex_1.getTokenRegex)(newTokenName, "css").test(fileSrc)) {
continue;
}
for (const [regexKey, regex] of Object.entries(config.regexes)) {
if (!regex) {
continue;
}
let match = regex.exec(fileSrc);
while (match) {
const { row, column } = getWordPositionInFile(fileSrc, match.index);
StatusStore.add({
isLegacy: false,
type: regexKey,
columnNumber: column,
lineNumber: row,
fileName,
name: match[0],
});
match = regex.exec(fileSrc);
}
}
}
if (action === "print") {
progressBar.update(index + 1, { fileName });
}
});
if (action === "no-print") {
return StatusStore;
}
progressBar.stop();
StatusStore.printStatus("summary");
StatusStore.printStatusForAll();
StatusStore.printMigrationHelp();
console.info("\n");
return StatusStore;
}
function getWordPositionInFile(fileContent, index) {
const lines = fileContent.split("\n");
let lineNumber = 1;
let charCount = 0;
for (let i = 0; i < lines.length; i++) {
const lineLength = lines[i].length + 1; // +1 to account for the newline character that was removed by split
if (charCount + lineLength > index) {
return { row: lineNumber, column: index - charCount + 1 };
}
charCount += lineLength;
lineNumber++;
}
return { row: lineNumber, column: 0 }; // Should not reach here if the index is within the file content range
}