@navikt/aksel
Version:
Aksel command line interface. Handles css-imports, codemods and more
231 lines (230 loc) • 9.31 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
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.runTooling = runTooling;
const chalk_1 = __importDefault(require("chalk"));
const enquirer_1 = __importDefault(require("enquirer"));
const fast_glob_1 = __importDefault(require("fast-glob"));
const jscodeshift = __importStar(require("jscodeshift/src/Runner"));
const path_1 = __importDefault(require("path"));
const codeshift_utils_1 = require("../codemod/codeshift.utils");
const validation_1 = require("../codemod/validation");
const print_remaining_1 = require("./tasks/print-remaining");
const status_1 = require("./tasks/status");
// Constants
const TRANSFORMS = {
"css-tokens": "./transforms/darkside-tokens-css",
"scss-tokens": "./transforms/darkside-tokens-scss",
"less-tokens": "./transforms/darkside-tokens-less",
"js-tokens": "./transforms/darkside-tokens-js",
"tailwind-tokens": "./transforms/darkside-tokens-tailwind",
};
const TASK_MENU = {
type: "select",
name: "task",
message: "Task",
initial: "status",
choices: [
{ message: "Check status", name: "status" },
{ message: "Print remaining tokens", name: "print-remaining-tokens" },
{ message: "Migrate CSS tokens", name: "css-tokens" },
{ message: "Migrate Scss tokens", name: "scss-tokens" },
{ message: "Migrate Less tokens", name: "less-tokens" },
{ message: "Migrate JS tokens", name: "js-tokens" },
{ message: "Migrate tailwind tokens", name: "tailwind-tokens" },
{ message: "Run all migrations", name: "run-all-migrations" },
{ message: "Exit", name: "exit" },
],
};
/**
* Main entry point for the tooling system
*/
function runTooling(options, program) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
// Find matching files based on glob pattern
const filepaths = fast_glob_1.default.sync([(_a = options.glob) !== null && _a !== void 0 ? _a : (0, codeshift_utils_1.getDefaultGlob)(options === null || options === void 0 ? void 0 : options.ext)], {
cwd: process.cwd(),
ignore: codeshift_utils_1.GLOB_IGNORE_PATTERNS,
});
if (options.dryRun) {
console.info(chalk_1.default.yellow("Running in dry-run mode, no changes will be made"));
}
// Show initial status
(0, status_1.getStatus)(filepaths);
// Task execution loop
let task = yield getNextTask();
while (task !== "exit") {
console.info("\n\n");
try {
yield executeTask(task, filepaths, options, program);
}
catch (error) {
program.error(chalk_1.default.red("Error:", error.message));
}
task = yield getNextTask();
}
process.exit(0);
});
}
/**
* Executes the selected task
*/
function executeTask(task, filepaths, options, program) {
return __awaiter(this, void 0, void 0, function* () {
switch (task) {
case "status":
(0, status_1.getStatus)(filepaths);
break;
case "print-remaining-tokens":
(0, print_remaining_1.printRemaining)(filepaths);
break;
case "css-tokens":
case "scss-tokens":
case "less-tokens":
case "js-tokens":
case "tailwind-tokens": {
const updatedStatus = (0, status_1.getStatus)(filepaths, "no-print").status;
const scopedFiles = getScopedFilesForTask(task, filepaths, updatedStatus);
yield runCodeshift(task, scopedFiles, {
dryRun: options.dryRun,
force: options.force,
});
break;
}
case "run-all-migrations": {
const tasks = [
"css-tokens",
"scss-tokens",
"less-tokens",
"js-tokens",
"tailwind-tokens",
];
for (const migrationTask of tasks) {
if (!options.force) {
(0, validation_1.validateGit)(options, program);
}
console.info(`\nRunning ${migrationTask}...`);
yield runCodeshift(migrationTask, filepaths, {
dryRun: options.dryRun,
force: true,
});
}
break;
}
default:
program.error(chalk_1.default.red(`Unknown task: ${task}`));
}
});
}
/**
* Filter files based on the selected task
*/
function getScopedFilesForTask(task, filepaths, status) {
return filepaths.filter((f) => {
switch (task) {
case "css-tokens":
return !!status.css.legacy.find((config) => config.fileName === f);
case "scss-tokens":
return !!status.scss.legacy.find((config) => config.fileName === f);
case "less-tokens":
return !!status.less.legacy.find((config) => config.fileName === f);
case "js-tokens":
return !!status.js.legacy.find((config) => config.fileName === f);
case "tailwind-tokens":
return !!status.tailwind.legacy.find((config) => config.fileName === f);
default:
return false;
}
});
}
/**
* Runs the jscodeshift codemod for the selected task
*/
function runCodeshift(task, filepaths, options) {
return __awaiter(this, void 0, void 0, function* () {
if (!TRANSFORMS[task]) {
throw new Error(`No transform found for task: ${task}`);
}
const codemodPath = path_1.default.join(__dirname, `${TRANSFORMS[task]}.js`);
yield jscodeshift.run(codemodPath, filepaths, {
babel: true,
ignorePattern: codeshift_utils_1.GLOB_IGNORE_PATTERNS,
parser: "tsx",
verbose: 2,
runInBand: true,
silent: false,
stdin: false,
dry: options === null || options === void 0 ? void 0 : options.dryRun,
force: options === null || options === void 0 ? void 0 : options.force,
print: false,
});
});
}
/**
* Prompts the user for the next task to run
*/
function getNextTask() {
return __awaiter(this, void 0, void 0, function* () {
try {
const response = yield enquirer_1.default.prompt([
Object.assign(Object.assign({}, TASK_MENU), { onCancel: () => process.exit(1) }),
]);
return response.task;
}
catch (error) {
if (error.isTtyError) {
console.info("Oops, something went wrong! Looks like @navikt/aksel can't run in this terminal. " +
"Contact Aksel for support if this persists, or try another terminal.");
}
else {
console.error(error);
}
process.exit(1);
}
});
}