Skip to content

Commit

Permalink
Merge pull request #2 from markschellhas/feature/sitemap
Browse files Browse the repository at this point in the history
Adds basic sitemap feature
  • Loading branch information
markschellhas authored Jan 31, 2024
2 parents f97b927 + ffb411e commit fefc5f0
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ or in your project directory:
npm install chic.js
```

## [1.4.0] - 2024-01-31

Adds sitemap generation command `chic sitemap`

## [1.3.0] - 2023-08-08

Adds funcitonality to hide `/routes` for production deployments by settings `CHIC_DEBUG=OFF` in the `.env` file.
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@ Chic.js adds a `/routes` endpoint to your app, which shows all the routes in you

| Command | Description |
| --- | --- |
| `chic --help` | Displays help information about Chic.js commands |
| `chic --version` | Displays the current version of Chic.js |
| `chic new GuitarStore` | Creates a new Sveltekit app, called `GuitarStore` |
| `chic new GuitarStore styled with tailwind` | Creates a new Sveltekit app, called `GuitarStore`, with Tailwind CSS styling framework. Options currently available: `bootstrap`, `tailwind` and `bulma` |
| `chic make Guitar name:string type:string description:text` | Creates pages, API routes, model and form components for CRUD operations on the `Guitar` resource |
| `chic add /about` | Creates an "About" page in the `src/routes` directory |
| `chic add ContactForm` | Creates a `ContactForm.svelte` component in the `src/lib/components` directory |
| `chic sitemap [domain name]` | Creates a sitemap (note: build your project locally first before running this command) |
| `chic s` | Runs the development server |
| `chic debug status` | Shows the status of `CHIC_DEBUG` in your `.env` file |
| `chic debug ON` | Sets `CHIC_DEBUG` value to `ON`. When `ON`, the routes endpoint will be active |
| `chic debug OFF` | Sets `CHIC_DEBUG` value to `OFF`. When `OFF`, the routes endpoint will be inactive |
| `chic --version` | Displays the current version of Chic.js |
| `chic --help` | Displays help information about Chic.js commands |
| `chic debug OFF` | Sets `CHIC_DEBUG` value to `OFF`. When `OFF`, the routes endpoint will be inactive |
15 changes: 13 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@ import sade from 'sade';

import {
addModelToDBFile, createAPIRoutes, createController,
createModel, createRoutePages, init, createFormComponent, writeDeleteButtonComponent, createHooksServerFile, readConfig, addNewRouteOrComponent, getDebugValue, setDebugValue
createModel, createRoutePages, init, createFormComponent, writeDeleteButtonComponent, createHooksServerFile, readConfig, addNewRouteOrComponent, getDebugValue, setDebugValue,
createSitemap
} from './lib/functions.js';
import { spawn } from 'child_process';
import { destroyButtonTemplate } from './lib/templates/component_templates.js';
import { CONSOLE_COLOR, styledBy } from './lib/helpers.js';
import { create } from 'domain';

const prog = sade('chic');

prog
.version('1.3.2')
.version('1.4.0')

prog
.command('new <name>')
Expand Down Expand Up @@ -235,6 +237,15 @@ prog

});

prog
.command('sitemap <url>')
.describe('Generates a sitemap.xml file for your project.')
.example('sitemap https://example.com')
.action((url) => {
createSitemap(url);
});


prog.parse(process.argv);

/**
Expand Down
85 changes: 84 additions & 1 deletion lib/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,90 @@ function createRouteServerPage(name, routePath) {
};

/**
* Creates a controller for a given input name and fields.
* Creates a site map for a given domain name.
* @param {string} domainName - The domain name.
* @returns {void} Nothing is returned.
*
* @todo Add a way to exclude routes from the sitemap.
*/
export function createSitemap(domainName) {
// const dirPath = path.join('src', 'routes');
const dirPath = path.join('.svelte-kit', 'output', 'server', 'entries', 'pages');
const filePath = path.join('static', 'sitemap.xml');

fs.readdir(dirPath, (err, files) => {
if (err) {
console.log(CONSOLE_COLOR.RED, "The sitemap could not be created because the build folder (.svelte-kit) could not be found. \n\nPlease run `npm run build` first.\n\n");
console.error('Error reading directory:', err);
console.log(CONSOLE_COLOR.BLUE, "\n\n💡 To solve this issue: run `npm run build` first. And then run `chic sitemap [domain name]` again.\n\n");
return;
}

let content = `<?xml version="1.0" encoding="UTF-8"?>\n`;
content += `<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">\n`;
content += ` <url>\n`;
content += ` <loc>${domainName}</loc>\n`;
content += ` <changefreq>monthly</changefreq>\n`;
content += ` <priority>1.0</priority>\n`;
content += ` </url>\n`;

// Create an array of promises for each file's stat operation
let statPromises = files.map(file => {
return new Promise((resolve, reject) => {
const fullPath = path.join(dirPath, file);

fs.stat(fullPath, (err, stats) => {
if (err) {
console.error('Error getting file stats:', err);
reject(err);
return;
}

if (stats.isDirectory() && file !== 'api') {
content += ` <url>\n`;
content += ` <loc>${domainName}/${file}</loc>\n`;
content += ` <changefreq>monthly</changefreq>\n`;
content += ` <priority>0.8</priority>\n`;
content += ` </url>\n`;
}

resolve();
});
});
});

// Wait for all stat operations to complete
Promise.all(statPromises).then(() => {
content += `</urlset>\n`;

// write file to static folder:
fs.writeFile(filePath, content, (err) => {
if (err) {
console.error(CONSOLE_COLOR.RED, 'Error creating file:', err);
} else {
console.log(CONSOLE_COLOR.GREEN, '- ' + filePath + ' created.');
}
});

// write file to build folder:
let buildOutputPath = path.join('.svelte-kit', 'output', 'client', 'sitemap.xml');
fs.writeFile(buildOutputPath, content, (err) => {
if (err) {
console.error(CONSOLE_COLOR.RED, 'Error creating file:', err);
} else {
console.log(CONSOLE_COLOR.GREEN, '- ' + buildOutputPath + ' created.');
}
});

}).catch(error => {
console.error('An error occurred:', error);
});
});
};


/**
* Creates a controller for a given inpdut name and fields.
* @param {string} inputName - The name of the input.
* @param {Array<Object>} fields - An array of objects representing the fields.
* @returns {void} Nothing is returned.
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "chic.js",
"version": "1.3.2",
"version": "1.4.0",
"description": "CLI for rapidly scaffolding SvelteKit apps",
"main": "index.js",
"bin": {
Expand Down

0 comments on commit fefc5f0

Please sign in to comment.