Skip to content

Commit

Permalink
fix: docs
Browse files Browse the repository at this point in the history
  • Loading branch information
zdm committed Apr 15, 2024
1 parent 3a38c9e commit 8544bae
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 24 deletions.
34 changes: 23 additions & 11 deletions lib/package/docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ export default class Docs {
return this.#rootPackage;
}

get locationPath () {
if ( !this.#rootPackage.cliConfig.docs ) {
return null;
}
else if ( !this.#rootPackage.cliConfig.docs.location ) {
return this.#rootPackage.root;
}
else {
return this.#rootPackage.root + "/" + this.#rootPackage.cliConfig.docs.location;
}
}

// XXX isExists -> isEnabled
get isExists () {
return !!this.#rootPackage.cliConfig.docs;
Expand Down Expand Up @@ -91,7 +103,7 @@ export default class Docs {
const fileTree = new FileTree();

// generate README.md
if ( !fs.existsSync( options.locationPath + "/README.md" ) ) {
if ( !fs.existsSync( this.locationPath + "/README.md" ) ) {
const readmeTmpl = utils.resolve( "#resources/templates/docs/README.md", import.meta.url );

if ( options.location ) {
Expand All @@ -114,11 +126,11 @@ export default class Docs {
}

// generate default sidebar
if ( !fs.existsSync( options.locationPath + "/_sidebar.md" ) ) {
if ( !fs.existsSync( this.locationPath + "/_sidebar.md" ) ) {
fileTree.add( { "path": "_sidebar.md", "buffer": await ejs.renderFile( utils.resolve( "#resources/templates/docs/_sidebar.md", import.meta.url ), options ) } );
}

await fileTree.write( options.locationPath );
await fileTree.write( this.locationPath );

// copy .nojekyll
this.#copyNoJekyll( options );
Expand All @@ -131,12 +143,12 @@ export default class Docs {
let index = await ejs.renderFile( utils.resolve( "#resources/templates/docs/index.html", import.meta.url ), options );

// lint index.html
const res = await new LintFile( new File( { "path": options.locationPath + "/" + "index.html", "buffer": index } ) ).run( "lint" );
const res = await new LintFile( new File( { "path": this.locationPath + "/" + "index.html", "buffer": index } ) ).run( "lint" );
if ( !res.ok ) throw res;
index = res.data;

// write index.html
fs.writeFileSync( options.locationPath + "/" + "index.html", index );
fs.writeFileSync( this.locationPath + "/" + "index.html", index );

// copy .nojekyll
this.#copyNoJekyll( options );
Expand Down Expand Up @@ -169,7 +181,7 @@ export default class Docs {
if ( !res.ok ) throw res;
const fileTree = res.data;

const location = options.locationPath + "/" + type;
const location = this.locationPath + "/" + type;

fs.rmSync( location, { "force": true, "recursive": true } );

Expand All @@ -190,7 +202,7 @@ export default class Docs {

// XXX replace relative urls
async #buildReadme ( options ) {
const readmePath = options.locationPath + "/README.md";
const readmePath = this.locationPath + "/README.md";

if ( options.location || options.generateReadme === false || !fs.existsSync( readmePath ) ) return;

Expand All @@ -207,19 +219,19 @@ export default class Docs {
}

#copyNoJekyll ( options ) {
fs.copyFileSync( utils.resolve( "#resources/templates/docs/.nojekyll", import.meta.url ), options.locationPath + "/.nojekyll" );
fs.copyFileSync( utils.resolve( "#resources/templates/docs/.nojekyll", import.meta.url ), this.locationPath + "/.nojekyll" );
}

async #checkMarkdown ( options ) {
const files = glob( "**/*.md", {
"cwd": options.locationPath,
"cwd": this.locationPath,
"directories": false,
} );

const warn = [];

for ( const file of files ) {
let markdown = fs.readFileSync( options.locationPath + "/" + file, "utf8" );
let markdown = fs.readFileSync( this.locationPath + "/" + file, "utf8" );

let changed;

Expand Down Expand Up @@ -268,7 +280,7 @@ export default class Docs {
}
}

if ( changed ) fs.writeFileSync( options.locationPath + "/" + file, markdown );
if ( changed ) fs.writeFileSync( this.locationPath + "/" + file, markdown );
}

return warn.join( "\n" );
Expand Down
13 changes: 0 additions & 13 deletions lib/package/docs/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,6 @@ export default class Options {
return new this( pkg, config, upstream );
}

get location () {
return this.#config.location;
}

get locationPath () {
if ( !this.location ) {
return this.#pkg.root;
}
else {
return this.#pkg.root + "/" + this.location;
}
}

get generateReadme () {
return this.#config.generateReadme;
}
Expand Down

0 comments on commit 8544bae

Please sign in to comment.