Skip to content

Commit

Permalink
Merge pull request #212 from jaredwray/adding-in-init-generation
Browse files Browse the repository at this point in the history
adding in init generation
  • Loading branch information
jaredwray authored Dec 5, 2023
2 parents 91d87e2 + 3309578 commit d231175
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 0 deletions.
1 change: 1 addition & 0 deletions init/favicon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added init/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions init/writr.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const writrOptions = {
templatePath: './template',
outputPath: './dist',
sitePath: './site',
githubPath: 'jaredwray/writr',
siteTitle: 'Writr',
siteDescription: 'Beautiful Website for Your Projects',
siteUrl: 'https://writr.org',
};

9 changes: 9 additions & 0 deletions init/writr.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const writrOptions = {
templatePath: './template',
outputPath: './dist',
sitePath: './site',
githubPath: 'jaredwray/writr',
siteTitle: 'Writr',
siteDescription: 'Beautiful Website for Your Projects',
siteUrl: 'https://writr.org',
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
},
"files": [
"dist",
"init",
"bin"
]
}
18 changes: 18 additions & 0 deletions src/writr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,24 @@ export default class Writr {
const files = fs.readdirSync(docsPath);
return files.length === 0;
}

public generateInit(sitePath: string, isTypescript: boolean): void {
// Check if the site path exists
if (!fs.existsSync(sitePath)) {
fs.mkdirSync(sitePath);
}

// Add the writr.config file based on js or ts
const writrConfigFile = isTypescript ? './init/writr.config.ts' : './init/writr.config.js';
fs.copyFileSync(writrConfigFile, `${sitePath}/writr.config.${isTypescript ? 'ts' : 'js'}`);

// Add in the image and favicon
fs.copyFileSync('./init/logo.png', `${sitePath}/logo.png`);
fs.copyFileSync('./init/favicon.svg', `${sitePath}/favicon.svg`);

// Output the instructions
this._console.log(`Writr initialized. Please update the ${writrConfigFile} file with your site information. In addition, you can replace the image, favicon, and stype the site with site.css file.`);
}
}

export {WritrHelpers} from './helpers.js';
49 changes: 49 additions & 0 deletions test/writr.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import process from 'node:process';
import {expect, it, describe} from 'vitest';
import fs from 'fs-extra';
import Writr, {WritrHelpers} from '../src/writr.js';
import {WritrOptions} from '../src/options.js';

Expand Down Expand Up @@ -68,4 +69,52 @@ describe('writr', () => {
expect(writr.isSinglePageWebsite(singlePageSite)).toEqual(true);
expect(writr.isSinglePageWebsite(multiPageSite)).toEqual(false);
});
it('should generate the site init files and folders', () => {
const writr = new Writr(defaultOptions);
const consoleLog = console.log;
let consoleMessage = '';
const temporarySitePath = './temp-site';
console.log = message => {
/* eslint-disable-next-line @typescript-eslint/no-unsafe-assignment */
consoleMessage = message;
};

try {
writr.generateInit(temporarySitePath, true);

expect(consoleMessage).toContain('Writr initialized.');
console.log = consoleLog;

expect(fs.existsSync(temporarySitePath)).toEqual(true);
expect(fs.existsSync(`${temporarySitePath}/writr.config.ts`)).toEqual(true);
expect(fs.existsSync(`${temporarySitePath}/logo.png`)).toEqual(true);
expect(fs.existsSync(`${temporarySitePath}/favicon.svg`)).toEqual(true);
} finally {
fs.rmdirSync(temporarySitePath, {recursive: true});
}
});
it('should generate the site init files and folders for javascript', () => {
const writr = new Writr(defaultOptions);
const consoleLog = console.log;
let consoleMessage = '';
const temporarySitePath = './temp-site-js';
console.log = message => {
/* eslint-disable-next-line @typescript-eslint/no-unsafe-assignment */
consoleMessage = message;
};

try {
writr.generateInit(temporarySitePath, false);

expect(consoleMessage).toContain('Writr initialized.');
console.log = consoleLog;

expect(fs.existsSync(temporarySitePath)).toEqual(true);
expect(fs.existsSync(`${temporarySitePath}/writr.config.js`)).toEqual(true);
expect(fs.existsSync(`${temporarySitePath}/logo.png`)).toEqual(true);
expect(fs.existsSync(`${temporarySitePath}/favicon.svg`)).toEqual(true);
} finally {
fs.rmdirSync(temporarySitePath, {recursive: true});
}
});
});

0 comments on commit d231175

Please sign in to comment.