Skip to content

Commit 077d5fb

Browse files
committed
feat: compile 命令的 filePath 参数默认值为 app 目录
1 parent 10f75c9 commit 077d5fb

File tree

4 files changed

+32
-10
lines changed

4 files changed

+32
-10
lines changed

bin/lcui.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ program
3131
program
3232
.command("compile")
3333
.description("Compile resource files into C source files")
34-
.argument("<filePath>", "File or directory")
34+
.argument("[filePath]", "File or directory")
3535
.option("--verbose", "More detailed log output")
3636
.action(wrapAction(compile));
3737

lib/compiler/index.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import UILoader from "./ui-loader.js";
1010
import XMLLoader from "./xml-loader.js";
1111
import YAMLLoader from "./yaml-loader.js";
1212
import JSONLoader from "./json-loader.js";
13+
import { resolveRootDir } from "../utils.js";
1314

1415
/** @type {Record<string, Loader>} */
1516
const loaderMap = {
@@ -24,7 +25,7 @@ const loaderMap = {
2425
};
2526

2627
function getDirs() {
27-
const rootContext = process.cwd();
28+
const rootContext = resolveRootDir();
2829
const mkdir = (dirPath) => {
2930
if (!fs.existsSync(dirPath)) {
3031
fs.mkdirpSync(dirPath);
@@ -445,11 +446,15 @@ export default async function compile(file, compilerOptions) {
445446
}
446447
}
447448

448-
if (!fs.existsSync(file)) {
449-
throw new Error(`${file}: no such file or directory`);
450-
}
451449
try {
452-
await compileFile(path.resolve(file));
450+
if (file) {
451+
if (!fs.existsSync(file)) {
452+
throw new Error(`${file}: no such file or directory`);
453+
}
454+
await compileFile(path.resolve(file));
455+
} else {
456+
await compileFile(options.appDir);
457+
}
453458
compiler.hooks.done.call();
454459
logger.info("Compilation completed!");
455460
} catch (err) {

src/compiler/css-loader.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ export default async function CSSLoader(
4343
})
4444
);
4545
}
46-
const customConfig = (await loadConfig(loader, "postcss")) as null | {
46+
const customConfig = (await loadConfig(
47+
path.dirname(loader.resourcePath),
48+
"postcss"
49+
)) as null | {
4750
plugins?: postcss.AcceptedPlugin[];
4851
};
4952
if (customConfig && Array.isArray(customConfig.plugins)) {

src/utils.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import path from "path";
2+
import fs from "fs-extra";
23
import { cosmiconfig } from "cosmiconfig";
3-
import { LoaderContext } from "./types.js";
44

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

25-
export async function loadConfig(loaderContext: LoaderContext, moduleName: string) {
25+
export async function loadConfig(context: string, moduleName: string) {
2626
/** @see https://github.com/webpack-contrib/postcss-loader/blob/b1aecd9b18ede38b0ad4e693a94dadd2b2531429/src/utils.js#L51 */
2727
const searchPlaces = [
2828
// Prefer popular format
@@ -48,9 +48,23 @@ export async function loadConfig(loaderContext: LoaderContext, moduleName: strin
4848
searchStrategy: "global",
4949
searchPlaces,
5050
});
51-
const result = await explorer.search(path.dirname(loaderContext.resourcePath));
51+
const result = await explorer.search(context);
5252
if (!result || result.isEmpty) {
5353
return null;
5454
}
5555
return result.config;
5656
}
57+
58+
export function resolveRootDir() {
59+
let dir = process.cwd();
60+
const configFiles = ["package.json", "lcui.config.js"];
61+
62+
do {
63+
if (configFiles.some((file) => fs.existsSync(path.join(dir, file)))) {
64+
return dir;
65+
}
66+
} while (dir);
67+
throw new Error(
68+
"Unable to determine the project root directory, please add the package.json file to the project root directory"
69+
);
70+
}

0 commit comments

Comments
 (0)