UNPKG

wails-auto-ipc

Version:

generate single app.go for wails app from multiple files

134 lines (131 loc) 4.21 kB
// @ts-check import { NeinthComponent } from 'neinth'; /** * @type {NeinthComponent< * void, * undefined * >} */ const neinthInstance = new NeinthComponent(async function () { this.generateWatcher({ relativePath: '/go_/', addDirToSet: false, addFileToSet: true, encoding: 'utf8', }); const templateApp_ = this.listenToNeinth('neinth-src/wails-auto-ipc/templateApp.mjs'); /** * @type {import('vivth').Signal<Set<{argsName:string[],funcName:string}>>} */ const setOfPartialReturnTypes_ = this.newSignal(new Set()); const watcher_ = this.listenToGeneratedWatcher(); let ipcs = []; this.new$(async ({ onBefore$ }) => { onBefore$(async () => { ipcs = []; setOfPartialReturnTypes_.nonReactiveValue.clear(); }); const watcher = watcher_.value; const templateApp = templateApp_.value; if (!watcher || !templateApp) { return; } watcher.infos.forEach((info) => { const { ext, baseName, content } = info; const extention = ext.noDot; const basename = baseName.noExt; if (extention !== 'go' || !templateApp.isValidExported(basename, content)) { return; } const res = templateApp.generateAppPartial(basename, content); const { appGoString, isFunc } = res; ipcs.push(appGoString); if (!isFunc) { return; } const { argsName, funcName } = res; setOfPartialReturnTypes_.nonReactiveValue.add({ argsName, funcName }); }); const string = templateApp.generateAppGoFull(ipcs); this.synchronizeFiles({ id: 'generate:app.go', SetOfFilesInstance: new this.SetOfFiles({ relativePathFromProjectRoot: 'app.go', template: { string: string, }, encoding: 'utf-8', }), }); setOfPartialReturnTypes_.call$(); }); const frontend_ = this.listenToNeinth('neinth-src/wails-auto-ipc/fronteend.watcher.mjs'); this.new$(async () => { const SetOfFilesInstance = new this.SetOfFiles(); const frontend = frontend_.value; const setOfPartialReturnTypes = setOfPartialReturnTypes_.value; if (!frontend || !setOfPartialReturnTypes) { return; } for await (const info of frontend.infos) { const { content, path, baseName } = info; const { relative } = path; const { withExt: baseNameWithExt } = baseName; switch (baseNameWithExt) { case 'App.js': case 'App.d.ts': let newContent = content; const relativePathFromProjectRoot = this.normalizePath(relative).replace( `wailsjs/go/main/${baseNameWithExt}`, `wails-auto-ipc/${baseNameWithExt}` ); setOfPartialReturnTypes.forEach(({ argsName, funcName }) => { const suppossedArgs = argsName.join(', '); if (baseNameWithExt == 'App.js') { const currentStringRegex = new RegExp( `export function\\s+${funcName}\\s*\\(([^)]*)\\)\\s*{([\\s\\S]*?)}`, 'm' ); const newString = `export function ${funcName}(${suppossedArgs}) { return window['go']['main']['App']['${funcName}'](${suppossedArgs}); }`; newContent = newContent.replace(currentStringRegex, newString); } else { const currentStringRegex = new RegExp( `export function ${funcName}\\(([^)]*)\\):Promise<(.+?)>;`, 'm' ); const matches = content.match(currentStringRegex); if (!matches) { return; } const [currentString, currentArgs, awaitedReturnType] = matches; const args_ = currentArgs.split(','); const newArgs = []; for (let i = 0; i < args_.length; i++) { const [_, argType] = args_[i].split(':'); newArgs.push(`${argsName[i]}:${argType}`); } const newString = `export function ${funcName}(${newArgs.join( ', ' )}):Promise<${awaitedReturnType}>;`; newContent = newContent.replace(currentString, newString); } }); SetOfFilesInstance.add({ relativePathFromProjectRoot, encoding: 'utf-8', template: { string: newContent, }, }); break; } } this.synchronizeFiles({ id: 'wailjsAutoFrontEnd', SetOfFilesInstance, }); }); }); export default neinthInstance;