Skip to content

Commit

Permalink
feat: compile 命令的 filePath 参数默认值为 app 目录
Browse files Browse the repository at this point in the history
  • Loading branch information
lc-soft committed Apr 22, 2024
1 parent 5d182b3 commit 8825ef8
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 10 deletions.
2 changes: 1 addition & 1 deletion bin/lcui.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ program
program
.command("compile")
.description("Compile resource files into C source files")
.argument("<filePath>", "File or directory")
.argument("[filePath]", "File or directory")
.option("--verbose", "More detailed log output")
.action(wrapAction(compile));

Expand Down
15 changes: 10 additions & 5 deletions lib/compiler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import UILoader from "./ui-loader.js";
import XMLLoader from "./xml-loader.js";
import YAMLLoader from "./yaml-loader.js";
import JSONLoader from "./json-loader.js";
import { resolveRootDir } from "../utils.js";

/** @type {Record<string, Loader>} */
const loaderMap = {
Expand All @@ -24,7 +25,7 @@ const loaderMap = {
};

function getDirs() {
const rootContext = process.cwd();
const rootContext = resolveRootDir();
const mkdir = (dirPath) => {
if (!fs.existsSync(dirPath)) {
fs.mkdirpSync(dirPath);
Expand Down Expand Up @@ -445,11 +446,15 @@ export default async function compile(file, compilerOptions) {
}
}

if (!fs.existsSync(file)) {
throw new Error(`${file}: no such file or directory`);
}
try {
await compileFile(path.resolve(file));
if (options.filePath) {
if (!fs.existsSync(file)) {
throw new Error(`${file}: no such file or directory`);
}
await compileFile(path.resolve(file));
} else {
await compileFile(options.appDir);
}
compiler.hooks.done.call();
logger.info("Compilation completed!");
} catch (err) {
Expand Down
5 changes: 4 additions & 1 deletion src/compiler/css-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ export default async function CSSLoader(
})
);
}
const customConfig = (await loadConfig(loader, "postcss")) as null | {
const customConfig = (await loadConfig(
path.dirname(loader.resourcePath),
"postcss"
)) as null | {
plugins?: postcss.AcceptedPlugin[];
};
if (customConfig && Array.isArray(customConfig.plugins)) {
Expand Down
20 changes: 17 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from "path";
import fs from "fs-extra";
import { cosmiconfig } from "cosmiconfig";
import { LoaderContext } from "./types.js";

export function toIdent(str) {
return str.replace(/[^a-zA-Z0-9]/g, "_");
Expand All @@ -22,7 +22,7 @@ export function parsePageRoute(context: string, filePath: string) {
};
}

export async function loadConfig(loaderContext: LoaderContext, moduleName: string) {
export async function loadConfig(context: string, moduleName: string) {
/** @see https://github.com/webpack-contrib/postcss-loader/blob/b1aecd9b18ede38b0ad4e693a94dadd2b2531429/src/utils.js#L51 */
const searchPlaces = [
// Prefer popular format
Expand All @@ -48,9 +48,23 @@ export async function loadConfig(loaderContext: LoaderContext, moduleName: strin
searchStrategy: "global",
searchPlaces,
});
const result = await explorer.search(path.dirname(loaderContext.resourcePath));
const result = await explorer.search(context);
if (!result || result.isEmpty) {
return null;
}
return result.config;
}

export function resolveRootDir() {
let dir = process.cwd();
const configFiles = ["package.json", "lcui.config.js"];

do {
if (configFiles.some((file) => fs.existsSync(path.join(dir, file)))) {
return dir;
}
} while (dir);
throw new Error(
"Unable to determine the project root directory, please add the package.json file to the project root directory"
);
}

0 comments on commit 8825ef8

Please sign in to comment.