@weapp-tailwindcss/cli 0.0.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,16 @@
1
+ # @weapp-tailwindcss/cli
2
+
3
+ ```json
4
+ {
5
+ "setting": {
6
+ "packNpmManually": true,
7
+ "packNpmRelationList": [
8
+ {
9
+ "packageJsonPath": "./package.json",
10
+ "miniprogramNpmDistDir": "./dist"
11
+ }
12
+ ]
13
+ },
14
+ "miniprogramRoot": "dist/"
15
+ }
16
+ ```
package/dist/index.cjs ADDED
@@ -0,0 +1,329 @@
1
+ 'use strict';
2
+
3
+ const commander = require('commander');
4
+ const loadPostcssConfig = require('postcss-load-config');
5
+ const path = require('node:path');
6
+ const gulp = require('weapp-tailwindcss/gulp');
7
+ const gulp$1 = require('gulp');
8
+ const fs = require('fs-extra');
9
+ const cosmiconfig = require('cosmiconfig');
10
+
11
+ function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
12
+
13
+ const loadPostcssConfig__default = /*#__PURE__*/_interopDefaultCompat(loadPostcssConfig);
14
+ const path__default = /*#__PURE__*/_interopDefaultCompat(path);
15
+ const gulp__default = /*#__PURE__*/_interopDefaultCompat(gulp$1);
16
+ const fs__default = /*#__PURE__*/_interopDefaultCompat(fs);
17
+
18
+ function isPlainObject(value) {
19
+ if (value === null || typeof value !== "object") {
20
+ return false;
21
+ }
22
+ const prototype = Object.getPrototypeOf(value);
23
+ if (prototype !== null && prototype !== Object.prototype && Object.getPrototypeOf(prototype) !== null) {
24
+ return false;
25
+ }
26
+ if (Symbol.iterator in value) {
27
+ return false;
28
+ }
29
+ if (Symbol.toStringTag in value) {
30
+ return Object.prototype.toString.call(value) === "[object Module]";
31
+ }
32
+ return true;
33
+ }
34
+
35
+ function _defu(baseObject, defaults, namespace = ".", merger) {
36
+ if (!isPlainObject(defaults)) {
37
+ return _defu(baseObject, {}, namespace, merger);
38
+ }
39
+ const object = Object.assign({}, defaults);
40
+ for (const key in baseObject) {
41
+ if (key === "__proto__" || key === "constructor") {
42
+ continue;
43
+ }
44
+ const value = baseObject[key];
45
+ if (value === null || value === void 0) {
46
+ continue;
47
+ }
48
+ if (merger && merger(object, key, value, namespace)) {
49
+ continue;
50
+ }
51
+ if (Array.isArray(value) && Array.isArray(object[key])) {
52
+ object[key] = [...value, ...object[key]];
53
+ } else if (isPlainObject(value) && isPlainObject(object[key])) {
54
+ object[key] = _defu(
55
+ value,
56
+ object[key],
57
+ (namespace ? `${namespace}.` : "") + key.toString(),
58
+ merger
59
+ );
60
+ } else {
61
+ object[key] = value;
62
+ }
63
+ }
64
+ return object;
65
+ }
66
+ function createDefu(merger) {
67
+ return (...arguments_) => (
68
+ // eslint-disable-next-line unicorn/no-array-reduce
69
+ arguments_.reduce((p, c) => _defu(p, c, "", merger), {})
70
+ );
71
+ }
72
+ const defu = createDefu();
73
+
74
+ var AssetType = /* @__PURE__ */ ((AssetType2) => {
75
+ AssetType2["JavaScript"] = "javascript";
76
+ AssetType2["Json"] = "json";
77
+ AssetType2["Css"] = "css";
78
+ AssetType2["Html"] = "html";
79
+ return AssetType2;
80
+ })(AssetType || {});
81
+
82
+ const defaultJavascriptExtensions = ["js"];
83
+ const defaultTypescriptExtensions = ["ts"];
84
+ const defaultWxsExtensions = ["wxs"];
85
+ const defaultNodeModulesDirs = ["**/node_modules/**", "**/miniprogram_npm/**", "**/project.config.json/**", "**/project.private.config.json/**", "**/package.json/**"];
86
+ async function build(options) {
87
+ const {
88
+ root: cwd,
89
+ weappTailwindcssOptions,
90
+ outDir,
91
+ src: srcBase,
92
+ clean,
93
+ extensions,
94
+ exclude,
95
+ include
96
+ } = defu(options, {
97
+ outDir: "dist",
98
+ weappTailwindcssOptions: {},
99
+ clean: true,
100
+ src: "",
101
+ exclude: [...defaultNodeModulesDirs],
102
+ extensions: {
103
+ javascript: [...defaultJavascriptExtensions, ...defaultTypescriptExtensions, ...defaultWxsExtensions],
104
+ html: ["wxml"],
105
+ css: ["wxss", "less", "sass", "scss"],
106
+ json: ["json"]
107
+ }
108
+ });
109
+ if (clean) {
110
+ const { deleteAsync } = await import('del');
111
+ const patterns = [outDir + "/**"];
112
+ await deleteAsync(patterns, { cwd, ignore: defaultNodeModulesDirs });
113
+ }
114
+ await fs.ensureDir(path__default.resolve(cwd, outDir));
115
+ const base = srcBase ? srcBase + "/" : "";
116
+ function getGlobs(type) {
117
+ const globs = [];
118
+ if (typeof include === "function") {
119
+ globs.push(...include(type).map((x) => x));
120
+ }
121
+ if (typeof exclude === "function") {
122
+ globs.push(...exclude(type).map((x) => "!" + x));
123
+ } else if (Array.isArray(exclude)) {
124
+ globs.push(...exclude.map((x) => "!" + x));
125
+ }
126
+ globs.push(`!${outDir}/**/*`);
127
+ return globs;
128
+ }
129
+ const { transformJs, transformWxml, transformWxss } = gulp.createPlugins(weappTailwindcssOptions);
130
+ function getJsTasks() {
131
+ const assetType = AssetType.JavaScript;
132
+ const globs = getGlobs(assetType);
133
+ return extensions[assetType].map((x) => {
134
+ return () => gulp__default.src([`${base}**/*.${x}`, ...globs], { cwd }).pipe(
135
+ transformJs({
136
+ babelParserOptions: x === "ts" ? {
137
+ plugins: ["typescript"]
138
+ } : void 0
139
+ })
140
+ ).pipe(
141
+ gulp__default.dest(outDir, {
142
+ cwd
143
+ })
144
+ );
145
+ });
146
+ }
147
+ function getJsonTasks() {
148
+ const assetType = AssetType.Json;
149
+ const globs = getGlobs(assetType);
150
+ return extensions[assetType].map((x) => {
151
+ return () => gulp__default.src([`${base}**/*.${x}`, ...globs], { cwd }).pipe(
152
+ gulp__default.dest(outDir, {
153
+ cwd
154
+ })
155
+ );
156
+ });
157
+ }
158
+ function getCssTasks() {
159
+ const assetType = AssetType.Css;
160
+ const globs = getGlobs(assetType);
161
+ return extensions[assetType].map((x) => {
162
+ return () => gulp__default.src([`${base}**/*.${x}`, ...globs], { cwd }).pipe(transformWxss()).pipe(
163
+ gulp__default.dest(outDir, {
164
+ cwd
165
+ })
166
+ );
167
+ });
168
+ }
169
+ function getHtmlTasks() {
170
+ const assetType = AssetType.Html;
171
+ const globs = getGlobs(assetType);
172
+ return extensions[assetType].map((x) => {
173
+ return () => gulp__default.src([`${base}**/*.${x}`, ...globs], { cwd }).pipe(transformWxml()).pipe(
174
+ gulp__default.dest(outDir, {
175
+ cwd
176
+ })
177
+ );
178
+ });
179
+ }
180
+ function copyOthers() {
181
+ if (Array.isArray(include)) {
182
+ return gulp__default.src(
183
+ include.map((x) => {
184
+ return `${base}${x}`;
185
+ }),
186
+ { cwd }
187
+ ).pipe(
188
+ gulp__default.dest(outDir, {
189
+ cwd
190
+ })
191
+ );
192
+ }
193
+ }
194
+ const tasks = [...getJsTasks(), ...getJsonTasks(), ...getCssTasks(), ...getHtmlTasks(), copyOthers];
195
+ for (const task of tasks) {
196
+ const s = task();
197
+ if (s) {
198
+ await new Promise((resolve, reject) => s.on("finish", resolve).on("error", reject));
199
+ }
200
+ }
201
+ }
202
+
203
+ function createConfigLoader(root) {
204
+ const explorer = cosmiconfig.cosmiconfigSync("weapp-tw");
205
+ function search(searchFrom = root) {
206
+ const searchFor = explorer.search(searchFrom);
207
+ if (searchFor) {
208
+ searchFor.config = defu(searchFor.config, getDefaultConfig(root));
209
+ return searchFor;
210
+ }
211
+ }
212
+ function load(filepath) {
213
+ return explorer.load(filepath);
214
+ }
215
+ return {
216
+ search,
217
+ load
218
+ };
219
+ }
220
+ function getDefaultConfig(root) {
221
+ return {
222
+ outDir: "dist",
223
+ root,
224
+ srcDir: "."
225
+ };
226
+ }
227
+ function defineConfig(options) {
228
+ return options;
229
+ }
230
+ function updateProjectConfig(options) {
231
+ const { root, dest } = options;
232
+ const projectConfigFilename = "project.config.json";
233
+ const projectConfigPath = path__default.resolve(root, projectConfigFilename);
234
+ if (fs__default.existsSync(projectConfigPath)) {
235
+ try {
236
+ const projectConfig = fs__default.readJSONSync(projectConfigPath);
237
+ projectConfig.miniprogramRoot = "dist/";
238
+ projectConfig.srcMiniprogramRoot = "dist/";
239
+ projectConfig.setting.packNpmManually = true;
240
+ if (Array.isArray(projectConfig.setting.packNpmRelationList)) {
241
+ const x = projectConfig.setting.packNpmRelationList.find((x2) => x2.packageJsonPath === "./package.json" && x2.miniprogramNpmDistDir === "./dist");
242
+ if (!x) {
243
+ projectConfig.setting.packNpmRelationList.push({
244
+ packageJsonPath: "./package.json",
245
+ miniprogramNpmDistDir: "./dist"
246
+ });
247
+ }
248
+ } else {
249
+ projectConfig.setting.packNpmRelationList = [
250
+ {
251
+ packageJsonPath: "./package.json",
252
+ miniprogramNpmDistDir: "./dist"
253
+ }
254
+ ];
255
+ }
256
+ fs__default.outputJSONSync(dest ?? projectConfigPath, projectConfig, {
257
+ spaces: 2
258
+ });
259
+ console.log(`\u2728 \u8BBE\u7F6E ${projectConfigFilename} \u914D\u7F6E\u6587\u4EF6\u6210\u529F!`);
260
+ } catch {
261
+ console.warn(`\u2728 \u8BBE\u7F6E ${projectConfigFilename} \u914D\u7F6E\u6587\u4EF6\u5931\u8D25!`);
262
+ }
263
+ } else {
264
+ console.warn(`\u2728 \u6CA1\u6709\u627E\u5230 ${projectConfigFilename} \u6587\u4EF6!`);
265
+ }
266
+ }
267
+ function initConfig(options) {
268
+ const { lang, root } = defu(options, { lang: "js", root: process.cwd() });
269
+ const configFilename = `weapp-tw.config.${lang ?? "js"}`;
270
+ const configPath = path__default.resolve(root, configFilename);
271
+ fs__default.ensureDirSync(root);
272
+ if (lang === "ts") {
273
+ fs__default.writeFileSync(
274
+ configPath,
275
+ `import { defineConfig } from '@weapp-tailwindcss/cli'
276
+
277
+ export default defineConfig({})
278
+ `,
279
+ "utf8"
280
+ );
281
+ } else {
282
+ fs__default.writeFileSync(
283
+ configPath,
284
+ `/** @type {import('@weapp-tailwindcss/cli').UserConfig} */
285
+ module.exports = {}
286
+ `,
287
+ "utf8"
288
+ );
289
+ }
290
+ console.log(`\u2728 ${configFilename} \u914D\u7F6E\u6587\u4EF6\uFF0C\u521D\u59CB\u5316\u6210\u529F!`);
291
+ updateProjectConfig({ root });
292
+ return configPath;
293
+ }
294
+
295
+ function createCli() {
296
+ const cwd = process.cwd();
297
+ const program = new commander.Command();
298
+ const configLoader = createConfigLoader(cwd);
299
+ const userDefinedConfig = configLoader.search(cwd);
300
+ program.command("dev").alias("serve").action(async () => {
301
+ });
302
+ program.command("build").action(async () => {
303
+ const postcssOptions = await loadPostcssConfig__default({
304
+ cwd
305
+ });
306
+ await build(
307
+ defu(userDefinedConfig?.config, {
308
+ root: cwd,
309
+ weappTailwindcssOptions: {
310
+ postcssOptions
311
+ }
312
+ })
313
+ );
314
+ });
315
+ program.command("init").action(() => {
316
+ initConfig({
317
+ root: cwd
318
+ });
319
+ });
320
+ return program;
321
+ }
322
+
323
+ function createWatcher(options) {
324
+ }
325
+
326
+ exports.createCli = createCli;
327
+ exports.createConfigLoader = createConfigLoader;
328
+ exports.createWatcher = createWatcher;
329
+ exports.defineConfig = defineConfig;
@@ -0,0 +1,31 @@
1
+ import { Command } from 'commander';
2
+ import { UserDefinedOptions } from 'weapp-tailwindcss';
3
+ import * as cosmiconfig from 'cosmiconfig';
4
+
5
+ declare function createCli(): Command;
6
+
7
+ interface CreateWatcherOptions {
8
+ weappTailwindcssOptions: UserDefinedOptions;
9
+ outDir: string;
10
+ }
11
+ declare function createWatcher(options?: Partial<CreateWatcherOptions>): void;
12
+
13
+ type WeappTwCosmiconfigResult = {
14
+ config: UserConfig;
15
+ filepath: string;
16
+ isEmpty?: boolean;
17
+ };
18
+ declare function createConfigLoader(root: string): {
19
+ search: (searchFrom?: string) => WeappTwCosmiconfigResult | undefined;
20
+ load: (filepath: string) => cosmiconfig.CosmiconfigResult;
21
+ };
22
+ type UserConfig = {
23
+ outDir?: string;
24
+ root?: string;
25
+ srcDir?: string;
26
+ weappTailwindcssOptions?: UserDefinedOptions;
27
+ clean?: boolean;
28
+ };
29
+ declare function defineConfig(options: UserConfig): UserConfig;
30
+
31
+ export { type UserConfig, createCli, createConfigLoader, createWatcher, defineConfig };
@@ -0,0 +1,31 @@
1
+ import { Command } from 'commander';
2
+ import { UserDefinedOptions } from 'weapp-tailwindcss';
3
+ import * as cosmiconfig from 'cosmiconfig';
4
+
5
+ declare function createCli(): Command;
6
+
7
+ interface CreateWatcherOptions {
8
+ weappTailwindcssOptions: UserDefinedOptions;
9
+ outDir: string;
10
+ }
11
+ declare function createWatcher(options?: Partial<CreateWatcherOptions>): void;
12
+
13
+ type WeappTwCosmiconfigResult = {
14
+ config: UserConfig;
15
+ filepath: string;
16
+ isEmpty?: boolean;
17
+ };
18
+ declare function createConfigLoader(root: string): {
19
+ search: (searchFrom?: string) => WeappTwCosmiconfigResult | undefined;
20
+ load: (filepath: string) => cosmiconfig.CosmiconfigResult;
21
+ };
22
+ type UserConfig = {
23
+ outDir?: string;
24
+ root?: string;
25
+ srcDir?: string;
26
+ weappTailwindcssOptions?: UserDefinedOptions;
27
+ clean?: boolean;
28
+ };
29
+ declare function defineConfig(options: UserConfig): UserConfig;
30
+
31
+ export { type UserConfig, createCli, createConfigLoader, createWatcher, defineConfig };
@@ -0,0 +1,31 @@
1
+ import { Command } from 'commander';
2
+ import { UserDefinedOptions } from 'weapp-tailwindcss';
3
+ import * as cosmiconfig from 'cosmiconfig';
4
+
5
+ declare function createCli(): Command;
6
+
7
+ interface CreateWatcherOptions {
8
+ weappTailwindcssOptions: UserDefinedOptions;
9
+ outDir: string;
10
+ }
11
+ declare function createWatcher(options?: Partial<CreateWatcherOptions>): void;
12
+
13
+ type WeappTwCosmiconfigResult = {
14
+ config: UserConfig;
15
+ filepath: string;
16
+ isEmpty?: boolean;
17
+ };
18
+ declare function createConfigLoader(root: string): {
19
+ search: (searchFrom?: string) => WeappTwCosmiconfigResult | undefined;
20
+ load: (filepath: string) => cosmiconfig.CosmiconfigResult;
21
+ };
22
+ type UserConfig = {
23
+ outDir?: string;
24
+ root?: string;
25
+ srcDir?: string;
26
+ weappTailwindcssOptions?: UserDefinedOptions;
27
+ clean?: boolean;
28
+ };
29
+ declare function defineConfig(options: UserConfig): UserConfig;
30
+
31
+ export { type UserConfig, createCli, createConfigLoader, createWatcher, defineConfig };
package/dist/index.mjs ADDED
@@ -0,0 +1,317 @@
1
+ import { Command } from 'commander';
2
+ import loadPostcssConfig from 'postcss-load-config';
3
+ import path from 'node:path';
4
+ import { createPlugins } from 'weapp-tailwindcss/gulp';
5
+ import gulp from 'gulp';
6
+ import fs, { ensureDir } from 'fs-extra';
7
+ import { cosmiconfigSync } from 'cosmiconfig';
8
+
9
+ function isPlainObject(value) {
10
+ if (value === null || typeof value !== "object") {
11
+ return false;
12
+ }
13
+ const prototype = Object.getPrototypeOf(value);
14
+ if (prototype !== null && prototype !== Object.prototype && Object.getPrototypeOf(prototype) !== null) {
15
+ return false;
16
+ }
17
+ if (Symbol.iterator in value) {
18
+ return false;
19
+ }
20
+ if (Symbol.toStringTag in value) {
21
+ return Object.prototype.toString.call(value) === "[object Module]";
22
+ }
23
+ return true;
24
+ }
25
+
26
+ function _defu(baseObject, defaults, namespace = ".", merger) {
27
+ if (!isPlainObject(defaults)) {
28
+ return _defu(baseObject, {}, namespace, merger);
29
+ }
30
+ const object = Object.assign({}, defaults);
31
+ for (const key in baseObject) {
32
+ if (key === "__proto__" || key === "constructor") {
33
+ continue;
34
+ }
35
+ const value = baseObject[key];
36
+ if (value === null || value === void 0) {
37
+ continue;
38
+ }
39
+ if (merger && merger(object, key, value, namespace)) {
40
+ continue;
41
+ }
42
+ if (Array.isArray(value) && Array.isArray(object[key])) {
43
+ object[key] = [...value, ...object[key]];
44
+ } else if (isPlainObject(value) && isPlainObject(object[key])) {
45
+ object[key] = _defu(
46
+ value,
47
+ object[key],
48
+ (namespace ? `${namespace}.` : "") + key.toString(),
49
+ merger
50
+ );
51
+ } else {
52
+ object[key] = value;
53
+ }
54
+ }
55
+ return object;
56
+ }
57
+ function createDefu(merger) {
58
+ return (...arguments_) => (
59
+ // eslint-disable-next-line unicorn/no-array-reduce
60
+ arguments_.reduce((p, c) => _defu(p, c, "", merger), {})
61
+ );
62
+ }
63
+ const defu = createDefu();
64
+
65
+ var AssetType = /* @__PURE__ */ ((AssetType2) => {
66
+ AssetType2["JavaScript"] = "javascript";
67
+ AssetType2["Json"] = "json";
68
+ AssetType2["Css"] = "css";
69
+ AssetType2["Html"] = "html";
70
+ return AssetType2;
71
+ })(AssetType || {});
72
+
73
+ const defaultJavascriptExtensions = ["js"];
74
+ const defaultTypescriptExtensions = ["ts"];
75
+ const defaultWxsExtensions = ["wxs"];
76
+ const defaultNodeModulesDirs = ["**/node_modules/**", "**/miniprogram_npm/**", "**/project.config.json/**", "**/project.private.config.json/**", "**/package.json/**"];
77
+ async function build(options) {
78
+ const {
79
+ root: cwd,
80
+ weappTailwindcssOptions,
81
+ outDir,
82
+ src: srcBase,
83
+ clean,
84
+ extensions,
85
+ exclude,
86
+ include
87
+ } = defu(options, {
88
+ outDir: "dist",
89
+ weappTailwindcssOptions: {},
90
+ clean: true,
91
+ src: "",
92
+ exclude: [...defaultNodeModulesDirs],
93
+ extensions: {
94
+ javascript: [...defaultJavascriptExtensions, ...defaultTypescriptExtensions, ...defaultWxsExtensions],
95
+ html: ["wxml"],
96
+ css: ["wxss", "less", "sass", "scss"],
97
+ json: ["json"]
98
+ }
99
+ });
100
+ if (clean) {
101
+ const { deleteAsync } = await import('del');
102
+ const patterns = [outDir + "/**"];
103
+ await deleteAsync(patterns, { cwd, ignore: defaultNodeModulesDirs });
104
+ }
105
+ await ensureDir(path.resolve(cwd, outDir));
106
+ const base = srcBase ? srcBase + "/" : "";
107
+ function getGlobs(type) {
108
+ const globs = [];
109
+ if (typeof include === "function") {
110
+ globs.push(...include(type).map((x) => x));
111
+ }
112
+ if (typeof exclude === "function") {
113
+ globs.push(...exclude(type).map((x) => "!" + x));
114
+ } else if (Array.isArray(exclude)) {
115
+ globs.push(...exclude.map((x) => "!" + x));
116
+ }
117
+ globs.push(`!${outDir}/**/*`);
118
+ return globs;
119
+ }
120
+ const { transformJs, transformWxml, transformWxss } = createPlugins(weappTailwindcssOptions);
121
+ function getJsTasks() {
122
+ const assetType = AssetType.JavaScript;
123
+ const globs = getGlobs(assetType);
124
+ return extensions[assetType].map((x) => {
125
+ return () => gulp.src([`${base}**/*.${x}`, ...globs], { cwd }).pipe(
126
+ transformJs({
127
+ babelParserOptions: x === "ts" ? {
128
+ plugins: ["typescript"]
129
+ } : void 0
130
+ })
131
+ ).pipe(
132
+ gulp.dest(outDir, {
133
+ cwd
134
+ })
135
+ );
136
+ });
137
+ }
138
+ function getJsonTasks() {
139
+ const assetType = AssetType.Json;
140
+ const globs = getGlobs(assetType);
141
+ return extensions[assetType].map((x) => {
142
+ return () => gulp.src([`${base}**/*.${x}`, ...globs], { cwd }).pipe(
143
+ gulp.dest(outDir, {
144
+ cwd
145
+ })
146
+ );
147
+ });
148
+ }
149
+ function getCssTasks() {
150
+ const assetType = AssetType.Css;
151
+ const globs = getGlobs(assetType);
152
+ return extensions[assetType].map((x) => {
153
+ return () => gulp.src([`${base}**/*.${x}`, ...globs], { cwd }).pipe(transformWxss()).pipe(
154
+ gulp.dest(outDir, {
155
+ cwd
156
+ })
157
+ );
158
+ });
159
+ }
160
+ function getHtmlTasks() {
161
+ const assetType = AssetType.Html;
162
+ const globs = getGlobs(assetType);
163
+ return extensions[assetType].map((x) => {
164
+ return () => gulp.src([`${base}**/*.${x}`, ...globs], { cwd }).pipe(transformWxml()).pipe(
165
+ gulp.dest(outDir, {
166
+ cwd
167
+ })
168
+ );
169
+ });
170
+ }
171
+ function copyOthers() {
172
+ if (Array.isArray(include)) {
173
+ return gulp.src(
174
+ include.map((x) => {
175
+ return `${base}${x}`;
176
+ }),
177
+ { cwd }
178
+ ).pipe(
179
+ gulp.dest(outDir, {
180
+ cwd
181
+ })
182
+ );
183
+ }
184
+ }
185
+ const tasks = [...getJsTasks(), ...getJsonTasks(), ...getCssTasks(), ...getHtmlTasks(), copyOthers];
186
+ for (const task of tasks) {
187
+ const s = task();
188
+ if (s) {
189
+ await new Promise((resolve, reject) => s.on("finish", resolve).on("error", reject));
190
+ }
191
+ }
192
+ }
193
+
194
+ function createConfigLoader(root) {
195
+ const explorer = cosmiconfigSync("weapp-tw");
196
+ function search(searchFrom = root) {
197
+ const searchFor = explorer.search(searchFrom);
198
+ if (searchFor) {
199
+ searchFor.config = defu(searchFor.config, getDefaultConfig(root));
200
+ return searchFor;
201
+ }
202
+ }
203
+ function load(filepath) {
204
+ return explorer.load(filepath);
205
+ }
206
+ return {
207
+ search,
208
+ load
209
+ };
210
+ }
211
+ function getDefaultConfig(root) {
212
+ return {
213
+ outDir: "dist",
214
+ root,
215
+ srcDir: "."
216
+ };
217
+ }
218
+ function defineConfig(options) {
219
+ return options;
220
+ }
221
+ function updateProjectConfig(options) {
222
+ const { root, dest } = options;
223
+ const projectConfigFilename = "project.config.json";
224
+ const projectConfigPath = path.resolve(root, projectConfigFilename);
225
+ if (fs.existsSync(projectConfigPath)) {
226
+ try {
227
+ const projectConfig = fs.readJSONSync(projectConfigPath);
228
+ projectConfig.miniprogramRoot = "dist/";
229
+ projectConfig.srcMiniprogramRoot = "dist/";
230
+ projectConfig.setting.packNpmManually = true;
231
+ if (Array.isArray(projectConfig.setting.packNpmRelationList)) {
232
+ const x = projectConfig.setting.packNpmRelationList.find((x2) => x2.packageJsonPath === "./package.json" && x2.miniprogramNpmDistDir === "./dist");
233
+ if (!x) {
234
+ projectConfig.setting.packNpmRelationList.push({
235
+ packageJsonPath: "./package.json",
236
+ miniprogramNpmDistDir: "./dist"
237
+ });
238
+ }
239
+ } else {
240
+ projectConfig.setting.packNpmRelationList = [
241
+ {
242
+ packageJsonPath: "./package.json",
243
+ miniprogramNpmDistDir: "./dist"
244
+ }
245
+ ];
246
+ }
247
+ fs.outputJSONSync(dest ?? projectConfigPath, projectConfig, {
248
+ spaces: 2
249
+ });
250
+ console.log(`\u2728 \u8BBE\u7F6E ${projectConfigFilename} \u914D\u7F6E\u6587\u4EF6\u6210\u529F!`);
251
+ } catch {
252
+ console.warn(`\u2728 \u8BBE\u7F6E ${projectConfigFilename} \u914D\u7F6E\u6587\u4EF6\u5931\u8D25!`);
253
+ }
254
+ } else {
255
+ console.warn(`\u2728 \u6CA1\u6709\u627E\u5230 ${projectConfigFilename} \u6587\u4EF6!`);
256
+ }
257
+ }
258
+ function initConfig(options) {
259
+ const { lang, root } = defu(options, { lang: "js", root: process.cwd() });
260
+ const configFilename = `weapp-tw.config.${lang ?? "js"}`;
261
+ const configPath = path.resolve(root, configFilename);
262
+ fs.ensureDirSync(root);
263
+ if (lang === "ts") {
264
+ fs.writeFileSync(
265
+ configPath,
266
+ `import { defineConfig } from '@weapp-tailwindcss/cli'
267
+
268
+ export default defineConfig({})
269
+ `,
270
+ "utf8"
271
+ );
272
+ } else {
273
+ fs.writeFileSync(
274
+ configPath,
275
+ `/** @type {import('@weapp-tailwindcss/cli').UserConfig} */
276
+ module.exports = {}
277
+ `,
278
+ "utf8"
279
+ );
280
+ }
281
+ console.log(`\u2728 ${configFilename} \u914D\u7F6E\u6587\u4EF6\uFF0C\u521D\u59CB\u5316\u6210\u529F!`);
282
+ updateProjectConfig({ root });
283
+ return configPath;
284
+ }
285
+
286
+ function createCli() {
287
+ const cwd = process.cwd();
288
+ const program = new Command();
289
+ const configLoader = createConfigLoader(cwd);
290
+ const userDefinedConfig = configLoader.search(cwd);
291
+ program.command("dev").alias("serve").action(async () => {
292
+ });
293
+ program.command("build").action(async () => {
294
+ const postcssOptions = await loadPostcssConfig({
295
+ cwd
296
+ });
297
+ await build(
298
+ defu(userDefinedConfig?.config, {
299
+ root: cwd,
300
+ weappTailwindcssOptions: {
301
+ postcssOptions
302
+ }
303
+ })
304
+ );
305
+ });
306
+ program.command("init").action(() => {
307
+ initConfig({
308
+ root: cwd
309
+ });
310
+ });
311
+ return program;
312
+ }
313
+
314
+ function createWatcher(options) {
315
+ }
316
+
317
+ export { createCli, createConfigLoader, createWatcher, defineConfig };
package/package.json ADDED
@@ -0,0 +1,58 @@
1
+ {
2
+ "name": "@weapp-tailwindcss/cli",
3
+ "version": "0.0.0-alpha.0",
4
+ "description": "The cli of weapp-tailwindcss",
5
+ "keywords": [],
6
+ "author": "",
7
+ "license": "ISC",
8
+ "sideEffects": false,
9
+ "type": "commonjs",
10
+ "exports": {
11
+ ".": {
12
+ "types": "./dist/index.d.ts",
13
+ "import": "./dist/index.mjs",
14
+ "require": "./dist/index.cjs"
15
+ }
16
+ },
17
+ "main": "./dist/index.cjs",
18
+ "module": "./dist/index.mjs",
19
+ "types": "./dist/index.d.ts",
20
+ "files": [
21
+ "dist"
22
+ ],
23
+ "dependencies": {
24
+ "commander": "^12.0.0",
25
+ "cosmiconfig": "^9.0.0",
26
+ "del": "^7.1.0",
27
+ "fs-extra": "^11.2.0",
28
+ "gulp": "^5.0.0",
29
+ "gulp-less": "^5.0.0",
30
+ "gulp-sass": "^5.1.0",
31
+ "postcss-load-config": "^5.0.3",
32
+ "weapp-tailwindcss": "3.2.0-alpha.1"
33
+ },
34
+ "devDependencies": {
35
+ "@types/fs-extra": "^11.0.4",
36
+ "@types/gulp": "^4.0.17",
37
+ "@types/gulp-less": "^0.0.36",
38
+ "@types/gulp-sass": "^5.0.4",
39
+ "@types/vinyl-fs": "^3.0.5",
40
+ "chokidar": "^3.6.0",
41
+ "defu": "^6.1.4",
42
+ "ts-morph": "^22.0.0",
43
+ "vinyl-fs": "^4.0.0"
44
+ },
45
+ "publishConfig": {
46
+ "access": "public",
47
+ "registry": "https://registry.npmjs.org"
48
+ },
49
+ "scripts": {
50
+ "dev": "unbuild --sourcemap",
51
+ "build": "unbuild",
52
+ "test": "vitest run",
53
+ "test:dev": "vitest",
54
+ "release": "pnpm publish",
55
+ "dev:watch": "node test/scripts/watch.js",
56
+ "sync": "cnpm sync @weapp-tailwindcss/cli"
57
+ }
58
+ }