@amaui/style
Version:
CSS in JS styling solution
199 lines (198 loc) • 8.11 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 (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = __importDefault(require("path"));
const yargs_1 = __importDefault(require("yargs"));
const url_1 = __importDefault(require("url"));
const fast_glob_1 = __importDefault(require("fast-glob"));
const is_1 = __importDefault(require("@amaui/utils/is"));
const isEnvironment_1 = __importDefault(require("@amaui/utils/isEnvironment"));
const getEnvironment_1 = __importDefault(require("@amaui/utils/getEnvironment"));
const try_1 = __importDefault(require("@amaui/utils/try"));
const parse_1 = __importDefault(require("@amaui/utils/parse"));
const merge_1 = __importDefault(require("@amaui/utils/merge"));
const css_1 = __importDefault(require("./css"));
let values = [];
class AmauiCli {
constructor() {
this.options_ = {};
}
get options() {
return this.options_;
}
set options(options) {
var _a, _b, _c;
const options_ = options || {};
if ((0, isEnvironment_1.default)('nodejs')) {
const wd = process.cwd();
if (options_.package && !path_1.default.isAbsolute(options_.package))
options_.package = path_1.default.resolve(options_.package);
const packagePath = options_.package || 'package.json';
const pkg = (0, try_1.default)(() => require(path_1.default.resolve(wd, packagePath))) || {};
const amauiStyleOptions = (0, try_1.default)(() => require(path_1.default.resolve(wd, 'amaui-style.options.js'))) || {};
if ((_a = amauiStyleOptions === null || amauiStyleOptions === void 0 ? void 0 : amauiStyleOptions.files) === null || _a === void 0 ? void 0 : _a.length)
this.fileSource = wd;
else if ((_c = (_b = pkg['amaui-style']) === null || _b === void 0 ? void 0 : _b.files) === null || _c === void 0 ? void 0 : _c.length)
this.fileSource = path_1.default.resolve(packagePath, '../');
else
this.fileSource = wd;
// amaui-style.options.js priority over package.json 'amaui-style'
const fileOptions = (0, merge_1.default)(amauiStyleOptions, pkg['amaui-style'], { merge: { array: true } });
// amaui options priority over file options values
this.options_ = (0, merge_1.default)(options_, fileOptions, { copy: true, merge: { array: true } });
}
}
async run(argv) {
const { _, imports, log, files: files_, package: package_ } = argv;
// Reset
values = [];
// Options
// 1 argument as files
let files = files_ || [(_.length ? (0, parse_1.default)(_[0]) : '')].filter(Boolean);
files = (0, is_1.default)('array', files) ? files : [files];
const optionsCli = {};
if (imports !== undefined)
optionsCli['imports'] = imports;
if (!!files.length)
optionsCli['files'] = files;
if (log !== undefined)
optionsCli['log'] = log;
if (package_ !== undefined)
optionsCli['package'] = package_;
this.options = optionsCli;
// Init
await this.init();
// Make
await this.make();
if (process.send)
process.send('done');
// On user cmdline cancel exit the process
process.on('SIGINT', async () => {
process.exit(1);
});
// Exit the process
process.exit(0);
}
async init() {
// Imports
await this.imports();
// Import all the files to setup the mainAmauiGroup
await this.initNode();
if (this.options.log)
console.log('\nAmaui style\n');
}
async make() {
if (this.options.log)
console.log();
if (values.length) {
if (this.options.log)
console.log(`Making ${values.length} css files\n`);
for (const value of values) {
const style = (0, css_1.default)(value.value, value.options);
// Make
await style.make();
}
}
else if (this.options.log)
console.log('0 css methods found');
if (this.options.log)
console.log();
}
async imports() {
if ((0, isEnvironment_1.default)('nodejs') && this.options.imports) {
const values_ = (0, is_1.default)('string', this.options.imports) ? [this.options.imports] : this.options.imports;
for (const value of values_) {
await this.importFile(value);
}
}
}
async initNode() {
// Setup for nodejs mostly by importing all the regexp files from options
if ((0, isEnvironment_1.default)('nodejs')) {
let filePaths = [];
// Import all test files
if ((0, is_1.default)('string', this.options.files))
filePaths.push(this.options.files);
else if ((0, is_1.default)('array', this.options.files))
filePaths.push(...this.options.files);
filePaths = filePaths.filter(Boolean).map(item => path_1.default.isAbsolute(item) ? item : path_1.default.join(this.fileSource, item));
await this.import(filePaths);
}
}
async import(files) {
if (!!(files === null || files === void 0 ? void 0 : files.length)) {
const filesPaths = (await (0, fast_glob_1.default)(files, { onlyFiles: true }))
.map(filePath => path_1.default.resolve(filePath));
for (const file of filesPaths) {
// Prepare the environment
this.prepareEnvironment();
await this.importFile(file);
}
}
}
async importFile(file) {
// Import a file or an import error
try {
require(file);
}
catch (error) {
try {
return path_1.default.isAbsolute(file) ? await Promise.resolve().then(() => __importStar(require(url_1.default.pathToFileURL(file)))) : Promise.resolve().then(() => __importStar(require(file)));
}
catch (error) {
throw error;
}
}
}
prepareEnvironment() {
// Window or Node
const env = (0, getEnvironment_1.default)();
env.css = (value, options) => {
values.push({ value, options });
};
}
}
const run = async (argv) => {
const amauiStyleCli = new AmauiCli();
await amauiStyleCli.run(argv);
};
yargs_1.default
.command({
command: '$0',
description: 'Amaui style css maker',
builder: (command => command
.options('imports', { type: 'array' })
.options('files', { type: 'array' })
.options('log', { type: 'boolean' })
.options('package', { type: 'string' })),
handler: run,
})
.help()
.version(false)
.parse();