Skip to content

Commit d2d5aac

Browse files
Merge branch 'rewrite/mdx-cli' into rewrite/kebab-case
2 parents 36bf44a + 90b718f commit d2d5aac

File tree

3 files changed

+54
-10
lines changed

3 files changed

+54
-10
lines changed

bin/args.js

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
const yargs = require('yargs');
2+
const validatePresentationMode = require('./validate/presentation-mode');
23

3-
// Validate and normalize.
4-
const validate = parser => {
4+
const validate = async parser => {
55
const { argv } = parser;
6-
const { mdx } = argv;
6+
const { src } = argv;
77

8-
return Promise.resolve({ mdx });
8+
return await validatePresentationMode(src);
99
};
1010

1111
const args = () =>
1212
yargs
13-
.usage(`Usage: spectacle -m <file>`)
13+
.usage(`Usage: spectacle -s <file>`)
1414

1515
// MDX File
16-
.option('mdx', {
17-
alias: 'm',
18-
describe: 'Path to mdx file from which a presentation will be generated.',
16+
.option('src', {
17+
alias: 's',
18+
describe: 'Path to a file from which a presentation will be generated.',
1919
default: 'slides.mdx',
20-
required: false,
2120
type: 'string'
2221
})
2322

bin/cli.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ const main = () =>
1111
const mdxFilePath = parsedInput.mdx;
1212
if (mdxFilePath) {
1313
actions.launchMDXServer(mdxFilePath);
14-
} else {
14+
}
15+
// add future actions here
16+
else {
1517
throw new Error('Unsupported action.');
1618
}
1719
})

bin/validate/presentation-mode.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const path = require('path');
2+
const fs = require('fs');
3+
4+
const isMDXFileType = extension => extension === '.mdx' || extension === '.md';
5+
6+
const fileExists = srcPath => {
7+
const absSrcPath = path.resolve(srcPath);
8+
return new Promise(resolve => {
9+
fs.exists(absSrcPath, exists => {
10+
resolve(exists);
11+
});
12+
});
13+
};
14+
15+
const validatePresentationMode = async src => {
16+
/* - the default action of the CLI is to boot up a presentation from a file
17+
* - src defaults to `slides.mdx`
18+
* - first, check to see if the file type is supported
19+
* - then check to see if the default or provided file exists
20+
*/
21+
22+
const validatedValue = {};
23+
const extension = path.extname(src);
24+
25+
if (isMDXFileType(extension)) {
26+
validatedValue['mdx'] = src;
27+
}
28+
// support other file types here
29+
else {
30+
throw new Error(`The file type ${extension} is not currently supported.`);
31+
}
32+
33+
const exists = await fileExists(src);
34+
if (!exists) {
35+
throw new Error(
36+
`A file cannot be found at the path "${src}". Remember that the default file is ./slides.mdx`
37+
);
38+
}
39+
40+
return validatedValue;
41+
};
42+
43+
module.exports = validatePresentationMode;

0 commit comments

Comments
 (0)