Skip to content

Commit a7ecb1b

Browse files
committed
use async/await
1 parent 2bd6f75 commit a7ecb1b

File tree

2 files changed

+67
-98
lines changed

2 files changed

+67
-98
lines changed

.github/workflows/test.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,11 @@ jobs:
5454
with:
5555
node-version: 16
5656

57-
- run: exit 0
58-
# - run: npm ci --ignore-scripts
57+
- run: npm ci --ignore-scripts
5958
# - run: npm run build --workspaces --if-present
6059

61-
# - run: |
60+
- run: |
61+
npm run build --workspace="@csstools/base-cli"
62+
npm run test --workspace="@csstools/base-cli"
6263
# npm run build --workspace="@csstools/base-cli" --workspace="@csstools/postcss-base-plugin"
6364
# npm run test --workspace="@csstools/base-cli" --workspace="@csstools/postcss-base-plugin"

packages/base-cli/src/index.ts

Lines changed: 63 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import fs from 'fs';
1+
import fsp from 'fs/promises';
22
import path from 'path';
33
import { parseArguments, SignalValue } from './args';
44
import postcss from 'postcss';
@@ -8,170 +8,138 @@ export * from './help';
88

99
type PluginCreatorOptions = Record<string, unknown> | null;
1010

11-
export function cli(plugin: PluginCreator<PluginCreatorOptions>, allowedPluginOpts: Array<string>, helpLogger: () => void) {
11+
export async function cli(plugin: PluginCreator<PluginCreatorOptions>, allowedPluginOpts: Array<string>, helpLogger: () => void) {
1212
// get process and plugin options from the command line
1313
const argo = parseArguments(process.argv.slice(2), allowedPluginOpts, helpLogger);
1414
if (argo === SignalValue.InvalidArguments) {
1515
process.exit(1);
1616
}
1717

18+
// Read from stdin and write to stdout
1819
if (argo.stdin && argo.stdout) {
19-
return getStdin().then((css) => {
20+
try {
21+
const css = await getStdin();
2022
if (!css) {
2123
helpLogger();
2224
process.exit(1);
2325
}
2426

25-
const result = postcss([plugin(argo.pluginOptions)]).process(css, {
27+
const result = await postcss([plugin(argo.pluginOptions)]).process(css, {
2628
from: 'stdin',
2729
to: 'stdout',
2830
map: argo.inlineMap ? { inline: true } : false,
2931
});
3032

31-
return result.css;
32-
}).then((result) => {
33-
process.stdout.write(result + (argo.inlineMap ? '\n' : ''));
34-
35-
process.exit(0);
36-
}).catch((error) => {
33+
process.stdout.write(result.css + (argo.inlineMap ? '\n' : ''));
34+
} catch (error) {
3735
console.error(argo.debug ? error : error.message);
3836

3937
process.exit(1);
40-
});
38+
}
39+
40+
return;
4141
}
4242

43+
// Read from stdin and write to a file
4344
if (argo.stdin) {
4445
let output = argo.output;
4546
if (!output && argo.outputDir) {
4647
output = path.join(argo.outputDir, 'output.css');
4748
}
4849

49-
return getStdin().then((css) => {
50+
try {
51+
const css = await getStdin();
5052
if (!css) {
5153
helpLogger();
5254
process.exit(1);
5355
}
5456

55-
const result = postcss([plugin(argo.pluginOptions)]).process(css, {
57+
const result = await postcss([plugin(argo.pluginOptions)]).process(css, {
5658
from: 'stdin',
5759
to: output,
5860
map: (argo.inlineMap || argo.externalMap) ? { inline: argo.inlineMap } : false,
5961
});
6062

6163
if (argo.externalMap && result.map) {
62-
return Promise.all([
63-
writeFile(output, result.css),
64-
writeFile(`${output}.map`, result.map.toString()),
65-
]).then(() => {
66-
console.log(`CSS was written to "${path.normalize(output)}"`);
67-
return;
68-
});
64+
await Promise.all([
65+
await fsp.writeFile(output, result.css + (argo.inlineMap ? '\n' : '')),
66+
await fsp.writeFile(`${output}.map`, result.map.toString()),
67+
]);
68+
} else {
69+
await fsp.writeFile(output, result.css + (argo.inlineMap ? '\n' : ''));
6970
}
7071

71-
return writeFile(output, result.css + (argo.inlineMap ? '\n' : ''));
72-
}).then(() => {
7372
console.log(`CSS was written to "${path.normalize(output)}"`);
74-
75-
process.exit(0);
76-
}).catch((error) => {
73+
} catch (error) {
7774
console.error(argo.debug ? error : error.message);
7875

7976
process.exit(1);
80-
});
77+
}
78+
79+
return;
8180
}
8281

82+
// Read from one or more files and write to stdout
8383
if (argo.stdout) {
84-
const outputs = argo.inputs.map((input) => {
85-
return {
86-
input: input,
87-
result: null,
88-
};
89-
});
90-
91-
return Promise.all(argo.inputs.map((input) => {
92-
return readFile(input).then((css) => {
93-
const result = postcss([plugin(argo.pluginOptions)]).process(css, {
84+
let allCss: Array<string> = [];
85+
try {
86+
allCss = await Promise.all(argo.inputs.map(async (input) => {
87+
const css = await fsp.readFile(input);
88+
const result = await postcss([plugin(argo.pluginOptions)]).process(css, {
9489
from: input,
9590
to: 'stdout',
9691
map: false,
9792
});
9893

9994
return result.css;
100-
}).then((result) => {
101-
outputs.find((output) => output.input === input).result = result;
102-
});
103-
})).then(() => {
104-
outputs.forEach((output) => {
105-
process.stdout.write(output.result);
106-
});
107-
108-
process.exit(0);
109-
}).catch((error) => {
95+
}));
96+
} catch (error) {
11097
console.error(argo.debug ? error : error.message);
11198

11299
process.exit(1);
113-
});
114-
}
115-
116-
return Promise.all(argo.inputs.map((input) => {
117-
let output = argo.output;
118-
if (argo.outputDir) {
119-
output = path.join(argo.outputDir, path.basename(input));
120100
}
121-
if (argo.replace) {
122-
output = input;
101+
102+
for (const css of allCss) {
103+
process.stdout.write(css);
123104
}
124105

125-
return readFile(input).then((css) => {
126-
return postcss([plugin(argo.pluginOptions)]).process(css, {
106+
return;
107+
}
108+
109+
// Read from one or more files and write to as many files
110+
try {
111+
await Promise.all(argo.inputs.map(async (input) => {
112+
let output = argo.output;
113+
if (argo.outputDir) {
114+
output = path.join(argo.outputDir, path.basename(input));
115+
}
116+
if (argo.replace) {
117+
output = input;
118+
}
119+
120+
const css = await fsp.readFile(input);
121+
const result = await postcss([plugin(argo.pluginOptions)]).process(css, {
127122
from: input,
128123
to: output,
129124
map: (argo.inlineMap || argo.externalMap) ? { inline: argo.inlineMap } : false,
130125
});
131-
}).then((result) => {
126+
132127
if (argo.externalMap && result.map) {
133-
return Promise.all([
134-
writeFile(output, result.css),
135-
writeFile(`${output}.map`, result.map.toString()),
136-
]).then(() => {
137-
console.log(`CSS was written to "${path.normalize(output)}"`);
138-
return;
139-
});
128+
await Promise.all([
129+
await fsp.writeFile(output, result.css + (argo.inlineMap ? '\n' : '')),
130+
await fsp.writeFile(`${output}.map`, result.map.toString()),
131+
]);
132+
} else {
133+
await fsp.writeFile(output, result.css + (argo.inlineMap ? '\n' : ''));
140134
}
141135

142-
return writeFile(output, result.css + (argo.inlineMap ? '\n' : '')).then(() => {
143-
console.log(`CSS was written to "${path.normalize(output)}"`);
144-
});
145-
});
146-
})).catch((error) => {
136+
console.log(`CSS was written to "${path.normalize(output)}"`);
137+
}));
138+
} catch (error) {
147139
console.error(argo.debug ? error : error.message);
148140

149141
process.exit(1);
150-
});
151-
}
152-
153-
function readFile (pathname: string): Promise<string> {
154-
return new Promise((resolve, reject) => {
155-
fs.readFile(pathname, 'utf8', (error, data) => {
156-
if (error) {
157-
reject(error);
158-
} else {
159-
resolve(data);
160-
}
161-
});
162-
});
163-
}
164-
165-
function writeFile (pathname: string, data: string): Promise<void> {
166-
return new Promise((resolve, reject) => {
167-
fs.writeFile(pathname, data, (error) => {
168-
if (error) {
169-
reject(error);
170-
} else {
171-
resolve();
172-
}
173-
});
174-
});
142+
}
175143
}
176144

177145
function getStdin(): Promise<string> {

0 commit comments

Comments
 (0)