Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | 8x 8x 8x 8x 8x 8x 4x 4x 4x 4x 4x 4x 6x 4x 4x 6x 4x 3x 3x 1x 4x 4x 1x 3x | import path from 'path';
import fs from 'fs-extra';
import { getLessFiles } from './getLessFiles';
import { executeLess, IOutputFile } from './executeLess';
export interface ICompileOption {
out: string;
combine?: string;
rmGlobal?: boolean;
excludeCss?: boolean;
}
export async function getProjectName() {
const pkgPath = path.resolve(process.cwd(), 'package.json');
let projectName = '';
Eif (fs.existsSync(pkgPath)) {
const pkg = await fs.readJSON(pkgPath);
projectName = pkg.name;
}
return projectName
}
export default async function compile(dir: string, option: ICompileOption) {
const { excludeCss, rmGlobal, combine, out, ...otherOpts } = option || {};
const inputDir = path.join(process.cwd(), dir);
try {
const projectName = await getProjectName();
const files: Array<string> = await getLessFiles(inputDir, excludeCss ? /\.(less)$/ : undefined);
const lessSource = await Promise.all(files.map(async (lessPath: string) => {
return executeLess(lessPath, { rmGlobal, ...otherOpts });
}));
if (combine) {
const outputCssFile = path.join(process.cwd(), combine);
const cssStr: Array<string> = lessSource.map((item: IOutputFile) => item.css);
if (!!cssStr.join('').trim()) {
await fs.outputFile(outputCssFile, cssStr.join(''));
await log(path.relative(process.cwd(), outputCssFile));
} else {
console.log(`🚧 \x1b[35m${projectName}\x1b[0m =>\x1b[33m No content is output.\x1b[0m`);
}
} else E{
const outputDir = path.join(process.cwd(), out);
await Promise.all(lessSource.map(async (item: IOutputFile) => {
return outputFile(item, inputDir, outputDir);
}));
}
} catch (error) {
console.log('error:', error);
}
}
export async function outputFile(data: IOutputFile, inputDir: string, outputDir: string) {
try {
const logPathIn = path.relative(process.cwd(), data.path);
data.path = data.path.replace(inputDir, outputDir).replace(/.less$/, '.css');
const logPathOut = path.relative(process.cwd(), data.path);
await log(logPathOut, logPathIn);
await fs.outputFile(data.path, data.css);
if (data.imports && data.imports.length > 0) {
// console.log('\x1b[35m imports-> \x1b[0m:', data.imports);
}
} catch (error) {
throw error;
}
}
export async function log(output: string, input?: string) {
const projectName = await getProjectName();
if (input) {
console.log(`♻️ \x1b[32m ${projectName}\x1b[0m: ${input} ┈> ${output}`);
} else {
console.log(`♻️ \x1b[32m ${projectName}\x1b[0m: Output one file: ┈> ${output}`);
}
} |