Skip to content

Commit a37dbf3

Browse files
authored
Merge pull request #4 from zerodevx/feature/add-save
Add new feature --save directly to <root>/sitemap.xml
2 parents 6223d84 + 6457976 commit a37dbf3

File tree

4 files changed

+33
-8
lines changed

4 files changed

+33
-8
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@ so that you can pipe it to do other cool stuff. CLI also allows you to pipe in B
5353
| -p | --priority | glob-priority pair (eg: foo/*.html=0.1) |
5454
| -f | --changefreq | glob-changefreq pair (eg: foo/*.html=daily) |
5555
| -n | --no-clean | disable clean URLs |
56-
| -s | --slash | add trailing slash to all URLs |
56+
| -l | --slash | add trailing slash to all URLs |
5757
| -t | --text | output as .TXT instead |
58+
| -s | --save | save output directly to file `<root>/sitemap.xml` |
5859
| -v | --verbose | be more verbose |
5960

6061

@@ -154,6 +155,10 @@ Run `npm run test`.
154155

155156
## Changelog
156157

158+
**v1.1.0** - 2019-08-18:
159+
* **BREAKING**: Trailing slash alias `-s` renamed to `-l`. Sorry. :cry:
160+
* Add feature save directly to file `<rootDir>/sitemap.xml` instead of `stdout`.
161+
157162
**v1.0.1** - 2019-08-16:
158163
* Bugfix - empty line at EOF in text mode.
159164

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "static-sitemap-cli",
33
"description": "Simple CLI to pre-generate XML sitemaps for static sites locally.",
4-
"version": "1.0.1",
4+
"version": "1.1.0",
55
"author": "Jason Lee <[email protected]>",
66
"bin": {
77
"static-sitemap-cli": "./bin/run",
@@ -10,8 +10,8 @@
1010
"bugs": "https://github.com/zerodevx/static-sitemap-cli/issues",
1111
"dependencies": {
1212
"@oclif/command": "^1.5.18",
13-
"@oclif/config": "^1.13.2",
14-
"@oclif/plugin-help": "^2.2.0",
13+
"@oclif/config": "^1.13.3",
14+
"@oclif/plugin-help": "^2.2.1",
1515
"fast-glob": "^3.0.4",
1616
"get-stdin": "^7.0.0",
1717
"js2xmlparser": "^4.0.0",

src/index.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const getStdin = require('get-stdin');
33
const fg = require('fast-glob');
44
const mm = require('micromatch');
55
const parser = require('js2xmlparser');
6+
const fs = require('fs');
67

78
class StaticSitemapCliCommand extends Command {
89

@@ -94,7 +95,11 @@ class StaticSitemapCliCommand extends Command {
9495
doubleQuotes: true
9596
}
9697
});
97-
this.log(sitemap);
98+
if (flags.save) {
99+
fs.writeFileSync(`${addSlash(flags.root)}sitemap.xml`, `${sitemap}\n`, 'utf-8');
100+
} else {
101+
this.log(sitemap);
102+
}
98103

99104
}
100105
}
@@ -118,8 +123,8 @@ StaticSitemapCliCommand.flags = {
118123
help: flags.help({char: 'h'}),
119124
root: flags.string({
120125
char: 'r',
121-
description: '[default: current] root working directory',
122-
default: '',
126+
description: 'root working directory',
127+
default: '.',
123128
}),
124129
match: flags.string({
125130
char: 'm',
@@ -143,7 +148,7 @@ StaticSitemapCliCommand.flags = {
143148
default: false,
144149
}),
145150
slash: flags.boolean({
146-
char: 's',
151+
char: 'l',
147152
description: 'add trailing slash to all URLs',
148153
default: false,
149154
exclusive: ['no-clean'],
@@ -154,6 +159,12 @@ StaticSitemapCliCommand.flags = {
154159
default: false,
155160
exclusive: ['priority', 'changefreq'],
156161
}),
162+
save: flags.boolean({
163+
char: 's',
164+
description: 'save output directly to file <root>/sitemap.xml',
165+
default: false,
166+
exclusive: ['text'],
167+
}),
157168
verbose: flags.boolean({
158169
char: 'v',
159170
description: 'be more verbose',

test/index.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const {expect, test} = require('@oclif/test');
22
const cmd = require('../src');
3+
const fs = require('fs');
34

45
describe('#index', () => {
56

@@ -58,6 +59,14 @@ describe('#index', () => {
5859
expect(ctx.stdout).to.contain('https://example.com/blog/mixed-1/</loc>');
5960
});
6061

62+
test
63+
.do(() => cmd.run(['https://example.com', '--root', 'test/test-site/about', '--save']))
64+
.it('saves to sitemap.xml', () => {
65+
let out = fs.readFileSync('test/test-site/about/sitemap.xml', 'utf-8');
66+
expect(out).to.contain('<loc>https://example.com</loc>');
67+
fs.unlinkSync('test/test-site/about/sitemap.xml');
68+
});
69+
6170

6271
/*
6372
test

0 commit comments

Comments
 (0)