Skip to content

Commit

Permalink
fix index.js on deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
greggman committed Dec 4, 2023
1 parent 229b8ec commit 189ec7e
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 51 deletions.
47 changes: 47 additions & 0 deletions build/copy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import path from 'path';
import fsPromise from 'fs/promises';
import chokidar from 'chokidar';

export function copy({watch, transformFn = v => v}) {
return new Promise(resolve => {
const outDir = 'out';

async function copyFile(srcFilename) {
const dstFilename = path.join(outDir, srcFilename);
const dirname = path.dirname(dstFilename);
try {
await fsPromise.stat(dirname);
} catch {
await fsPromise.mkdir(dirname, { recursive: true });
}
console.log('copy', srcFilename, '->', dstFilename);
const src = await fsPromise.readFile(srcFilename);
const dst = transformFn(src, srcFilename, dstFilename);
await fsPromise.writeFile(dstFilename, dst);
}

chokidar.watch('.', {
ignored: [
'.git',
'node_modules',
'build',
'out',
'src',
'dist',
'**/.*',
],
}).on('all', (event, path) => {
switch (event) {
case 'add':
case 'change':
copyFile(path);
break;
case 'ready':
if (!watch) {
resolve();
}
break;
}
});
});
}
18 changes: 18 additions & 0 deletions build/prep-for-deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,25 @@ import path from 'path';
import * as url from 'url';
const dirname = url.fileURLToPath(new URL('.', import.meta.url));


const ignoreFilename = path.join(dirname, '..', '.gitignore');
const ignore = fs.readFileSync(ignoreFilename, {encoding: 'utf8'});
const newIgnore = ignore.replace(/# -- clip-for-deploy-start --[\s\S]*?# -- clip-for-deploy-end --/, '');
fs.writeFileSync(ignoreFilename, newIgnore);

const version = parseInt(JSON.parse(fs.readFileSync('package.json', {encoding: 'utf8'})).version);

function transformJS(src) {
return src.replace(/'.*?'\s+\/*\s+muigui-include\s+*\//g, `dist/${version}.x/muigui.module.js`);
}

[
'examples/js/index.js',
].forEach(filename => {
const src = fs.readFileSync(filename, {encoding: 'utf8'});
const dst = transformJS(src);
if (src !== dst) {
fs.writeFileSync(filename, dst);
}
});

49 changes: 2 additions & 47 deletions build/serve.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import path from 'path';
import fsPromise from 'fs/promises';
import {spawn} from 'child_process';
import chokidar from 'chokidar';
import {copy} from './copy.js';

spawn('./node_modules/.bin/tsc', [
'--watch',
Expand All @@ -17,47 +15,4 @@ spawn('./node_modules/.bin/servez', [
stdio: 'inherit',
});

const ignoreFns = [
fn => fn.startsWith('.git'),
fn => fn.startsWith('node_modules'),
fn => fn.startsWith('build'),
fn => fn.startsWith('out'),
fn => fn.startsWith('src'),
fn => fn.startsWith('dist'),
fn => fn.startsWith('.'),
];

function ignore(fn) {
for (const ignoreFn of ignoreFns) {
if (ignoreFn(fn)) {
return true;
}
}
return false;
}

const outDir = 'out';

async function copyFile(srcFilename) {
const dstFilename = path.join(outDir, srcFilename);
const dirname = path.dirname(dstFilename);
try {
await fsPromise.stat(dirname);
} catch {
await fsPromise.mkdir(dirname, { recursive: true });
}
console.log('copy', srcFilename, '->', dstFilename);
await fsPromise.copyFile(srcFilename, dstFilename);
}

chokidar.watch('.').on('all', (event, path) => {
switch (event) {
case 'add':
case 'change':
if (ignore(path)) {
return;
}
copyFile(path);
break;
}
});
copy({watch: true});
2 changes: 1 addition & 1 deletion examples/js/index/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as twgl from '../../3rdParty/twgl-full.module.js';
import VSAEffect from './VSAEffect.js';
import effects from './effects.js';
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import GUI, { helpers, Direction, TextNumber } from '../../../src/esm.js';
import GUI, { helpers, Direction, TextNumber } from '../../../src/esm.js'; /* muigui-include */

const canvas = document.querySelector('#bg');
const gl = canvas.getContext('webgl');
Expand Down
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,7 @@ <h2>Form Theme:</h2>
pointer-events: none;
}
#forkongithub a {
pointer-events: initial;
background: #000;
color: #fff;
text-decoration: none;
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"name": "muigui",
"version": "0.0.14",
"description": "A Simple GUI",
"main": "muigui.js",
"module": "src/muigui.js",
"main": "dist/0.x/muigui.js",
"module": "dist/0.x/muigui-module.js",
"type": "module",
"scripts": {
"build": "npm run build-normal",
Expand Down Expand Up @@ -33,7 +33,7 @@
"url": "https://github.com/greggman/muigui/issues"
},
"files": [
"muigui.js",
"dist/**/*",
"src/**/*"
],
"homepage": "https://github.com/greggman/muiguiy#readme",
Expand Down

0 comments on commit 189ec7e

Please sign in to comment.