Skip to content

Commit

Permalink
refactor: update documentation builder
Browse files Browse the repository at this point in the history
  • Loading branch information
dimaslanjaka committed May 5, 2023
1 parent e410809 commit 6c9cccc
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions page-build.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,45 @@
const path = require('path');
const fs = require('fs-extra');
const { default: gch, spawn } = require('git-command-helper');
const spawnAsync = require('cross-spawn').async;

// publish pages

const testDir = path.resolve(__dirname, 'test');
const src = path.join(testDir, 'public');
const dest = path.resolve(__dirname, '../dimaslanjaka.github.io/docs/hexo-shortcodes');
const deployDir = path.resolve(testDir, '.deploy_git/hexo-shortcodes');
(async function () {
// build start
await spawn('hexo', ['generate'], { cwd: testDir, shell: true });
// check deploy directory
if (!fs.existsSync(deployDir)) {
await spawn('git', ['clone', '-b', 'master', 'https://github.com/dimaslanjaka/docs.git', 'test/.deploy_git'], {
cwd: __dirname
});
} else {
await spawn('git', ['pull', 'origin', 'master', '-X', 'theirs'], { cwd: deployDir, shell: true, stdio: 'inherit' });
}

// build site start
await spawn('yarn', ['build'], { cwd: testDir, shell: true, stdio: 'inherit' });

// commit start
const projectGit = new gch(__dirname, 'master');
const commit = await projectGit.latestCommit(__dirname);
const repo = (await projectGit.getremote()).fetch.url;
const repo = new URL((await projectGit.getremote()).fetch.url).pathname.replace(/^\/+/gm, '');
const message = `chore: update from ${repo}@${commit}`;

// copy start
if (fs.existsSync(dest)) {
await fs.rm(dest, { recursive: true, force: true });
await fs.mkdir(dest);
if (fs.existsSync(deployDir)) {
await fs.rm(deployDir, { recursive: true, force: true });
await fs.mkdir(deployDir);
}
await fs.copy(src, dest, { overwrite: true });
await fs.copy(src, deployDir, { overwrite: true });

try {
const destSpawnOpt = { cwd: dest, shell: true, stdio: 'inherit' };
await spawn('git', ['remote', 'set-url', 'origin', 'https://github.com/dimaslanjaka/docs.git'], destSpawnOpt);
const destSpawnOpt = { cwd: deployDir, shell: true, stdio: 'inherit' };
//await spawn('git', ['remote', 'set-url', 'origin', 'https://github.com/dimaslanjaka/docs.git'], destSpawnOpt);
await spawn('git', ['add', '.'], destSpawnOpt);
await spawn('git', ['commit', '-m', `"${message}"`], destSpawnOpt);
await spawn('git', ['checkout', 'master'], destSpawnOpt);
await spawn('git', ['pull', 'origin', 'master', '-X', 'ours'], destSpawnOpt);
await spawn('git', ['push', '-u', 'origin', 'master'], destSpawnOpt);
} catch (e) {
console.error('cannot push', e);
Expand Down

0 comments on commit 6c9cccc

Please sign in to comment.