Skip to content

Commit

Permalink
feat(create): provide helpful tip when adding markdown support to exi…
Browse files Browse the repository at this point in the history
…sting vite config
  • Loading branch information
mihar-22 committed Feb 24, 2022
1 parent aa9e162 commit da9e098
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
45 changes: 43 additions & 2 deletions packages/create/bin/create-vitebook.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,6 @@ async function main() {
fs.mkdirSync(targetDir);
}

console.log(targetDir);

const builder = new ProjectBuilder({
targetDir,
link,
Expand Down Expand Up @@ -219,6 +217,49 @@ async function main() {
addPrettierFeature(builder);
addLintStagedFeature(builder);

if (builder.hasFeature('markdown')) {
let ext = '.js';
let viteFileContent = builder.dirs.dest.root.readFile('vite.config.js');

if (viteFileContent.length === 0) {
ext = '.ts';
viteFileContent = builder.dirs.dest.root.readFile('vite.config.ts');
}

let frameworkPluginName = '@sveltejs/vite-plugin-svelte';
let includeStatement = `\n{\n ${kleur.bold(
"extensions: ['.svelte', '.md']",
)}\n}`;

switch (builder.framework) {
case 'vue':
frameworkPluginName = '@vitejs/plugin-vue';
includeStatement = `\n{\n ${kleur.bold('include: /\\.(md|vue)$/')}\n}`;
break;
case 'preact':
frameworkPluginName = '@preact/preset-vite';
includeStatement = `\n{\n ${kleur.bold(
'include: /\\.([j|t]sx?|md)$/',
)}\n}`;
break;
}

const hasOwnFrameworkPlugin = viteFileContent.includes(frameworkPluginName);

if (hasOwnFrameworkPlugin) {
console.warn(
kleur.yellow(
`\nDetected ${kleur.bold(frameworkPluginName)} in ${kleur.bold(
`vite.config${ext}`,
)}`,
),
'\n\nTo complete adding markdown support, add the following line to the plugin options:\n',
includeStatement,
'\n\n',
);
}
}

// -------------------------------------------------------------------------------------------
// Configuration File
// -------------------------------------------------------------------------------------------
Expand Down
8 changes: 8 additions & 0 deletions packages/create/src/FileDirectory.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ export class FileDirectory {
return path.resolve(this.path, ...segments);
}

/**
* @param {string} filePath
*/
readFile(filePath) {
const path = this.resolve(filePath);
return fs.existsSync(path) ? fs.readFileSync(path).toString() : '';
}

/**
* @param {string} filePath
* @param {string} content
Expand Down

0 comments on commit da9e098

Please sign in to comment.