Skip to content

Commit 8de0c0d

Browse files
committed
feat: accept configuration file ⚙️
1 parent 9bdb96e commit 8de0c0d

File tree

8 files changed

+97
-19
lines changed

8 files changed

+97
-19
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules
2-
demo
2+
demo
3+
.vscode

README.md

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,74 @@
22

33
📦 This project give the possibility to generate a Google codelab with asciidoc or markdown files.
44

5-
You can use this module directly from the command line
5+
You can use this module directly from the command line or from your code !
66

7+
## How to use ?
8+
9+
### Command line
10+
11+
```shell
12+
npx @dxdeveloperexperience/codelab-generator ./index.adoc ./target
13+
14+
npx @dxdeveloperexperience/codelab-generator ./index.md ./target
15+
```
16+
17+
### In your code
18+
19+
```js
20+
const codelab = require("@dxdeveloperexperience/codelab-generator");
21+
22+
// ...
23+
await codelab(labTmpFile, labsOutputDir);
24+
```
25+
26+
## Configuration
27+
28+
### Values
29+
30+
| Name | Values | Description |
31+
| ---- | ------ | --------------------------------------------- |
32+
| base | "" | The base url of bower_components and elements |
33+
34+
### Examples
35+
36+
From shell
737
```shell
8-
npx @dxdeveloperexperience/codelab-generator ./index.adoc ./target
38+
npx @dxdeveloperexperience/codelab-generator ./index.adoc ./target path/to/config.json
39+
40+
npx @dxdeveloperexperience/codelab-generator ./index.md ./target path/to/config.json
41+
```
42+
43+
the config.json
44+
45+
```json
46+
{
47+
"base": "baseurl/test/"
48+
}
49+
```
50+
51+
or
52+
53+
```js
54+
const codelab = require("@dxdeveloperexperience/codelab-generator");
55+
56+
// ...
57+
await codelab(labTmpFile, labsOutputDir, { base: "" });
58+
```
59+
60+
Result, it will convert template
61+
from:
62+
63+
```html
64+
<script src="{{ &base }}bower_components/webcomponentsjs/webcomponents-lite.js"></script>
65+
<link rel="import" href="{{ &base }}elements/codelab.html" />
66+
```
67+
68+
to:
969

10-
npx @dxdeveloperexperience/codelab-generator ./index.md ./target
70+
```html
71+
<script src="baseurl/test/bower_components/webcomponentsjs/webcomponents-lite.js"></script>
72+
<link rel="import" href="baseurl/test/elements/codelab.html" />
1173
```
1274

1375
## Contributors

src/cli.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,28 @@
11
#!/usr/bin/env node
22

3+
const path = require("path");
34
const [, , ...args] = process.argv;
5+
let config = {};
46

5-
if (args.length !== 2) {
6-
console.error("You should define two arguments to this command line");
7+
if (args.length < 2) {
8+
console.error(
9+
"You should define a minimum of two arguments to this command line"
10+
);
711
process.exit();
812
}
913

10-
const path = require("path");
14+
if (args.length === 3) {
15+
const configPath = path.resolve(process.cwd(), args[3]);
16+
17+
try {
18+
config = JSON.parse(fs.readFileSync(configPath, "utf8"));
19+
} catch (error) {
20+
console.log(error);
21+
process.exit();
22+
}
23+
}
24+
1125
const input = path.resolve(process.cwd(), args[0]);
1226
const output = path.resolve(process.cwd(), args[1]);
1327

14-
require("./index")(input, output);
28+
require("./index")(input, output, config);

src/convert.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module.exports = function(input) {
1+
module.exports = function(input, config) {
22
const fs = require("fs");
33
const path = require("path");
44
const Mustache = require("mustache");
@@ -13,5 +13,5 @@ module.exports = function(input) {
1313
const convert = input.endsWith(".adoc")
1414
? require("./converter/adoc")
1515
: require("./converter/markdown");
16-
return Mustache.render(template, convert(body));
16+
return Mustache.render(template, convert(body, config));
1717
};

src/converter/adoc.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const { JSDOM } = require("jsdom");
22

3-
module.exports = function(body) {
3+
module.exports = function(body, config) {
44
const asciidoctor = require("asciidoctor")();
55
const content = asciidoctor.convert(body, {
66
attributes: { showtitle: true }
@@ -22,6 +22,7 @@ module.exports = function(body) {
2222
}
2323

2424
return (data = {
25+
...config,
2526
content: steps.join("\n"),
2627
title: dom.window.document.querySelector("h1").innerHTML
2728
});

src/converter/markdown.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const { JSDOM } = require("jsdom");
22

3-
module.exports = function(body) {
3+
module.exports = function (body, config) {
44
const showdown = require("showdown"),
55
converter = new showdown.Converter();
66
const content = converter.makeHtml(body.toString());
@@ -31,7 +31,8 @@ module.exports = function(body) {
3131
}
3232

3333
return {
34+
...config,
3435
content: steps.join("\n"),
35-
title: dom.window.document.querySelector("h1").innerHTML
36+
title: dom.window.document.querySelector("h1").innerHTML,
3637
};
3738
};

src/index.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
module.exports = function(input, target) {
1+
module.exports = function (input, target, config) {
22
const fs = require("fs");
33

4-
const html = require("./convert")(input);
4+
const html = require("./convert")(input, config);
55

66
const rimraf = require("rimraf");
77

@@ -17,7 +17,6 @@ module.exports = function(input, target) {
1717
return mkdir$(target);
1818
})
1919
.then(() => {
20-
console.log("downloading template");
2120
return git$()
2221
.silent(true)
2322
.clone(
@@ -28,6 +27,6 @@ module.exports = function(input, target) {
2827

2928
p.then(() => {
3029
return fs.writeFileSync(target + "/index.html", html);
31-
}).catch(err => console.error(err));
30+
}).catch((err) => console.error(err));
3231
return p;
3332
};

src/template.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
<meta name="theme-color" content="#4F7DC9" />
1010
<meta charset="UTF-8" />
1111
<title>{{ title }}</title>
12-
<script src="/bower_components/webcomponentsjs/webcomponents-lite.js"></script>
13-
<link rel="import" href="/elements/codelab.html" />
12+
<script src="{{ &base }}bower_components/webcomponentsjs/webcomponents-lite.js"></script>
13+
<link rel="import" href="{{ &base }}elements/codelab.html" />
1414
<link
1515
rel="stylesheet"
1616
href="//fonts.googleapis.com/css?family=Source+Code+Pro:400|Roboto:400,300,400italic,500,700|Roboto+Mono"

0 commit comments

Comments
 (0)