Skip to content

Commit 5531eb7

Browse files
committed
Add command use to set css-doodle version quickly
1 parent 6b62cb9 commit 5531eb7

2 files changed

Lines changed: 26 additions & 3 deletions

File tree

bin/handler.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ export async function handleSetConfig(field, value) {
111111
}
112112
}
113113

114+
export async function handleUseAction(version) {
115+
handleSetConfig('css-doodle', version);
116+
}
117+
114118
export function handleDisplayConfig(field) {
115119
console.log(config[field]);
116120
}
@@ -166,21 +170,33 @@ function readFromStdin() {
166170

167171
async function fetchCssDoodleSource(version) {
168172
let result = '', res, error;
173+
174+
if (/^css\-doodle@/.test(version)) {
175+
version = version.split('@')[1];
176+
}
177+
169178
const messageInvalid = `error: invalid package version '${version}'`;
179+
const messageFailed = `error: failed to fetch css-doodle@${version}`;
170180

171181
if (!(version === 'latest' || /^\d+\.\d+\.\d+$/.test(version))) {
172182
return {
173183
result, error: new Error(messageInvalid)
174184
}
175185
}
176186

177-
console.log(`Fetching css-doodle@${version}`);
187+
console.log(`Fetching css-doodle@${version} from esm.sh`);
178188

179189
try {
180-
res = await fetch(`https://esm.sh/css-doodle@${version}/css-doodle.min.js?raw`, { redirect:'follow' });
190+
res = await fetch(`https://esm.sh/css-doodle@${version}/css-doodle.min.js?raw`, { redirect: 'follow' });
181191
} catch (error) {
192+
try {
193+
console.log(`Try jsdelivr.net`);
194+
res = await fetch (`https://cdn.jsdelivr.net/npm/css-doodle@${version}/css-doodle.min.js`, { redirect: 'follow' });
195+
} catch (error) {
196+
return { result, error: new Error(messageFailed) }
197+
}
182198
return {
183-
result, error: new Error(`error: failed to fetch css-doodle@${version}`)
199+
result, error: new Error(messageFailed)
184200
};
185201
}
186202

bin/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
handleSetConfig,
1313
handleDisplayConfig,
1414
handleDisplayConfigList,
15+
handleUseAction,
1516
getProgramName
1617
} from './handler.js';
1718

@@ -87,6 +88,12 @@ const commandConfig = program
8788
.description('List all configurations')
8889
.action(handleDisplayConfigList);
8990

91+
program
92+
.command('use')
93+
.description('Shorthand to fetch and use a custom version of css-doodle')
94+
.argument('<version>', 'Custom version of css-doodle to use. E.g. css-doodle@latest')
95+
.action(handleUseAction);
96+
9097
program
9198
.command('parse')
9299
.description('Print the parsed tokens, helped to debug on development')

0 commit comments

Comments
 (0)