Skip to content

Commit

Permalink
Merge pull request #4 from zerodevx/feature/add-save
Browse files Browse the repository at this point in the history
Add new feature --save directly to <root>/sitemap.xml
  • Loading branch information
zerodevx authored Aug 18, 2019
2 parents 6223d84 + 6457976 commit a37dbf3
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ so that you can pipe it to do other cool stuff. CLI also allows you to pipe in B
| -p | --priority | glob-priority pair (eg: foo/*.html=0.1) |
| -f | --changefreq | glob-changefreq pair (eg: foo/*.html=daily) |
| -n | --no-clean | disable clean URLs |
| -s | --slash | add trailing slash to all URLs |
| -l | --slash | add trailing slash to all URLs |
| -t | --text | output as .TXT instead |
| -s | --save | save output directly to file `<root>/sitemap.xml` |
| -v | --verbose | be more verbose |


Expand Down Expand Up @@ -154,6 +155,10 @@ Run `npm run test`.

## Changelog

**v1.1.0** - 2019-08-18:
* **BREAKING**: Trailing slash alias `-s` renamed to `-l`. Sorry. :cry:
* Add feature save directly to file `<rootDir>/sitemap.xml` instead of `stdout`.

**v1.0.1** - 2019-08-16:
* Bugfix - empty line at EOF in text mode.

Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "static-sitemap-cli",
"description": "Simple CLI to pre-generate XML sitemaps for static sites locally.",
"version": "1.0.1",
"version": "1.1.0",
"author": "Jason Lee <[email protected]>",
"bin": {
"static-sitemap-cli": "./bin/run",
Expand All @@ -10,8 +10,8 @@
"bugs": "https://github.com/zerodevx/static-sitemap-cli/issues",
"dependencies": {
"@oclif/command": "^1.5.18",
"@oclif/config": "^1.13.2",
"@oclif/plugin-help": "^2.2.0",
"@oclif/config": "^1.13.3",
"@oclif/plugin-help": "^2.2.1",
"fast-glob": "^3.0.4",
"get-stdin": "^7.0.0",
"js2xmlparser": "^4.0.0",
Expand Down
19 changes: 15 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const getStdin = require('get-stdin');
const fg = require('fast-glob');
const mm = require('micromatch');
const parser = require('js2xmlparser');
const fs = require('fs');

class StaticSitemapCliCommand extends Command {

Expand Down Expand Up @@ -94,7 +95,11 @@ class StaticSitemapCliCommand extends Command {
doubleQuotes: true
}
});
this.log(sitemap);
if (flags.save) {
fs.writeFileSync(`${addSlash(flags.root)}sitemap.xml`, `${sitemap}\n`, 'utf-8');
} else {
this.log(sitemap);
}

}
}
Expand All @@ -118,8 +123,8 @@ StaticSitemapCliCommand.flags = {
help: flags.help({char: 'h'}),
root: flags.string({
char: 'r',
description: '[default: current] root working directory',
default: '',
description: 'root working directory',
default: '.',
}),
match: flags.string({
char: 'm',
Expand All @@ -143,7 +148,7 @@ StaticSitemapCliCommand.flags = {
default: false,
}),
slash: flags.boolean({
char: 's',
char: 'l',
description: 'add trailing slash to all URLs',
default: false,
exclusive: ['no-clean'],
Expand All @@ -154,6 +159,12 @@ StaticSitemapCliCommand.flags = {
default: false,
exclusive: ['priority', 'changefreq'],
}),
save: flags.boolean({
char: 's',
description: 'save output directly to file <root>/sitemap.xml',
default: false,
exclusive: ['text'],
}),
verbose: flags.boolean({
char: 'v',
description: 'be more verbose',
Expand Down
9 changes: 9 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const {expect, test} = require('@oclif/test');
const cmd = require('../src');
const fs = require('fs');

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

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

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


/*
test
Expand Down

0 comments on commit a37dbf3

Please sign in to comment.