fix(tauri.js) determine app path correctly (#493)

* fix(tauri.js) determine app path correctly

* fix(lint): replace backtick with single-quote

* fix(tests) mock app-paths

* fix(tauri.js) linting

* fix(test) use local tauri on empty fixture

* fix(tauri.js) linting

* fix(workflows) use local tauri-bundler test-tauri-js-cli

* fix(workflows) wrong cargo install command

* fix(tauri) comment webview-spawning test

Co-authored-by: nothingismagick <denjell@sfosc.org>
This commit is contained in:
Lucas Fernandes Nogueira
2020-03-09 21:27:54 -03:00
committed by GitHub
parent b95319bd74
commit 37afc5b0dc
14 changed files with 68 additions and 43 deletions

View File

@@ -70,7 +70,7 @@ jobs:
run: |
sudo apt-get update
sudo apt-get install -y webkit2gtk-4.0
- run: cargo install tauri-bundler --force
- run: cargo install --path ./cli/tauri-bundler --force
- name: test
timeout-minutes: 15
run: |

View File

@@ -42,7 +42,7 @@ module.exports = {
'../../package.json': '<rootDir>/package.json'
},
"transform": {
"templates[\\\\/]tauri\.js": "./test/jest/raw-loader-transformer.js",
"templates[\\\\/](tauri|mutation-observer)\.js": "./test/jest/raw-loader-transformer.js",
"\\.(js|ts)$": "babel-jest"
}
}

View File

@@ -176,7 +176,8 @@ module.exports = () => {
printInfo({ key: 'App directory structure', section: true })
const tree = dirTree(appDir)
for (const artifact of tree.children ?? []) {
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
for (const artifact of tree.children || []) {
if (artifact.type === 'folder') {
console.log(`/${artifact.name}`)
}

View File

@@ -25,7 +25,6 @@ import sharp from 'sharp'
import { appDir, tauriDir } from '../helpers/app-paths'
import logger from '../helpers/logger'
import * as settings from '../helpers/tauricon.config'
import nonWebpackRequire from '../helpers/non-webpack-require'
const log = logger('app:spawn')
const warn = logger('app:spawn', 'red')
@@ -180,7 +179,7 @@ const tauricon = (exports.tauricon = {
return typeof image === 'object'
},
version: function() {
return nonWebpackRequire('../../package.json').version
return require('../../package.json').version
},
make: async function(
src: string = path.resolve(appDir, 'app-icon.png'),
@@ -228,7 +227,8 @@ const tauricon = (exports.tauricon = {
try {
const pngImage = sharpSrc.resize(pvar[1], pvar[1])
if (pvar[2]) {
const rgb = hexToRgb(options.background_color) ?? {
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
const rgb = hexToRgb(options.background_color) || {
r: undefined,
g: undefined,
b: undefined
@@ -293,7 +293,8 @@ const tauricon = (exports.tauricon = {
) {
let output
let block = false
const rgb = hexToRgb(options.background_color) ?? {
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
const rgb = hexToRgb(options.background_color) || {
r: undefined,
g: undefined,
b: undefined

View File

@@ -1,5 +1,5 @@
import { ensureDirSync, writeFileSync } from 'fs-extra'
import { template } from 'lodash'
import { template } from 'lodash'
import path from 'path'
import { TauriConfig } from './types/config'

View File

@@ -1,21 +1,23 @@
import { existsSync } from 'fs'
import { join, normalize, resolve, sep } from 'path'
import logger from './logger'
const warn = logger('tauri', 'red')
const getAppDir = (): string => {
let dir = process.cwd()
let count = 0
// only go up three folders max
while (dir.length > 0 && dir.endsWith(sep) && count <= 2) {
if (existsSync(join(dir, 'tauri.conf.json'))) {
while (dir.length > 0 && !dir.endsWith(sep) && count <= 2) {
if (existsSync(join(dir, 'src-tauri', 'tauri.conf.json'))) {
return dir
}
count++
dir = normalize(join(dir, '..'))
}
// just return the current directory
return process.cwd()
warn('Couldn\'t find recognize the current folder as a part of a Tauri project')
process.exit(1)
}
const appDir = getAppDir()

View File

@@ -6,12 +6,13 @@ import { JSDOM } from 'jsdom'
import { debounce, template } from 'lodash'
import path from 'path'
import * as entry from './entry'
import { appDir, tauriDir } from './helpers/app-paths'
import { tauriDir } from './helpers/app-paths'
import logger from './helpers/logger'
import onShutdown from './helpers/on-shutdown'
import { spawn, spawnSync } from './helpers/spawn'
const getTauriConfig = require('./helpers/tauri-config')
import { TauriConfig } from './types/config'
// eslint-disable-next-line @typescript-eslint/no-var-requires
const getTauriConfig = require('./helpers/tauri-config')
const log = logger('app:tauri', 'green')
const warn = logger('app:tauri (runner)', 'red')
@@ -95,6 +96,7 @@ class Runner {
]
}
// eslint-disable-next-line security/detect-non-literal-fs-filename
this.tauriWatcher = chokidar
.watch(
[
@@ -138,7 +140,7 @@ class Runner {
const [command, ...args] = cfg.build.beforeBuildCommand.split(' ')
spawnSync(command, args, tauriDir)
}
const tomlContents = this.__getManifest()
this.__whitelistApi(cfg, tomlContents)
this.__rewriteManifest(tomlContents)
@@ -188,15 +190,16 @@ class Runner {
reject(new Error('Could not find index.html in dist dir.'))
}
const rewriteHtml = (html: string, interceptor?: (dom: JSDOM) => void) => {
const rewriteHtml = (html: string, interceptor?: (dom: JSDOM) => void): void => {
const dom = new JSDOM(html)
const document = dom.window.document
if (interceptor !== undefined) {
interceptor(dom)
}
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
if (!((cfg.ctx.dev && cfg.build.devPath.startsWith('http')) || cfg.tauri.embeddedServer.active)) {
const mutationObserverTemplate = require('!!raw-loader!!../templates/mutation-observer').default
const mutationObserverTemplate = require('../templates/mutation-observer').default
const compiledMutationObserver = template(mutationObserverTemplate)
const bodyMutationObserverScript = document.createElement('script')
@@ -276,7 +279,7 @@ class Runner {
}: {
cargoArgs: string[]
extraArgs?: string[]
dev?: boolean,
dev?: boolean
exitOnPanic?: boolean
}): Promise<void> {
return new Promise((resolve, reject) => {
@@ -298,7 +301,7 @@ class Runner {
warn()
warn('⚠️ [FAIL] Cargo CLI has failed')
warn()
reject()
reject(new Error('Cargo failed with status code ' + code.toString()))
process.exit(1)
} else if (!dev) {
resolve()
@@ -317,7 +320,7 @@ class Runner {
resolve()
}
)
if (dev) {
resolve()
}
@@ -355,7 +358,7 @@ class Runner {
return tomlContents
}
__rewriteManifest(tomlContents: JsonMap) {
__rewriteManifest(tomlContents: JsonMap): void {
const tomlPath = this.__getManifestPath()
const output = toml.stringify(tomlContents)
writeFileSync(tomlPath, output)
@@ -371,12 +374,12 @@ class Runner {
tomlFeatures.push('all-api')
} else {
const toKebabCase = (value: string): string => {
return value.replace(/([a-z])([A-Z])/g, "$1-$2")
return value.replace(/([a-z])([A-Z])/g, '$1-$2')
.replace(/\s+/g, '-')
.toLowerCase()
}
const whitelist = Object.keys(cfg.tauri.whitelist).filter(
w => cfg.tauri.whitelist[String(w)] === true
w => cfg.tauri.whitelist[String(w)]
)
tomlFeatures.push(...whitelist.map(toKebabCase))
}

View File

@@ -62,8 +62,14 @@ Run \`tauri init --force template\` to overwrite.`)
if (!force) return false
}
const resolveTauriPath = (tauriPath: string): string => {
const resolvedPath = tauriPath.startsWith('/') || /^\S:/g.test(tauriPath)
? join(tauriPath, 'tauri') // we received a full path as argument
: join('..', tauriPath, 'tauri') // we received a relative path
return resolvedPath.replace(/\\/g, '/')
}
const tauriDep = tauriPath
? `{ path = "${join('..', tauriPath, 'tauri')}" }`
? `{ path = "${resolveTauriPath(tauriPath)}" }`
: null
try {

View File

@@ -1,4 +1,3 @@
// eslint-disable-next-line node/no-missing-require
const { tauri } = require('bin/tauri')
describe('[CLI] tauri.js', () => {

View File

@@ -1,4 +1,7 @@
const tauricon = require('~/dist/api/tauricon.js')
const appTestSetup = require('../fixtures/app-test-setup')
appTestSetup.initJest('app')
const tauricon = require('api/tauricon')
describe('[CLI] tauri-icon internals', () => {
it('tells you the version', () => {

View File

@@ -13,12 +13,13 @@ describe('[CLI] tauri.js template', () => {
process.chdir(fixturePath)
if (existsSync(tauriFixturePath)) {
rmdirSync(tauriFixturePath, { recursive: true })
}
const init = require('api/init')
init({
directory: process.cwd(),
force: true,
tauriPath: resolve(__dirname, '../../../../..')
})
const { tauri } = require('bin/tauri')
tauri('init')
process.chdir(tauriFixturePath)
const manifestPath = resolve(tauriFixturePath, 'Cargo.toml')

View File

@@ -1,13 +1,11 @@
const path = require('path')
const process = require('process')
const fixtureDir = path.resolve(__dirname, '../fixtures')
const mockFixtureDir = path.resolve(__dirname, '../fixtures')
module.exports.fixtureDir = fixtureDir
module.exports.fixtureDir = mockFixtureDir
import * as appPaths from '../../../src/helpers/app-paths'
module.exports.initJest = (fixture) => {
module.exports.initJest = (mockFixture) => {
jest.setTimeout(240000)
jest.mock('helpers/non-webpack-require', () => {
return path => {
@@ -18,10 +16,21 @@ module.exports.initJest = (fixture) => {
return value
}
})
appPaths.appDir = path.join(fixtureDir, fixture)
appPaths.tauriDir = path.join(appPaths.appDir, 'src-tauri')
jest.spyOn(appPaths.resolve, 'app').mockImplementation(dir => path.join(appPaths.appDir, dir))
jest.spyOn(appPaths.resolve, 'tauri').mockImplementation(dir => path.join(appPaths.tauriDir, dir))
jest.mock('helpers/app-paths', () => {
const path = require('path')
const appDir = path.join(mockFixtureDir, mockFixture)
const tauriDir = path.join(appDir, 'src-tauri')
return {
appDir,
tauriDir,
resolve: {
app: dir => path.join(appDir, dir),
tauri: dir => path.join(tauriDir, dir)
}
}
})
jest.spyOn(process, 'exit').mockImplementation(() => {})
}

View File

@@ -21,7 +21,7 @@ module.exports = {
exclude: /node_modules/
},
{
test: /templates[\\/]tauri\.js/,
test: /templates[\\/](tauri|mutation-observer)\.js/,
use: 'raw-loader'
}
]

View File

@@ -128,7 +128,7 @@ mod test {
)
}
#[test]
/* #[test]
#[cfg(not(any(target_os = "linux", target_os = "macos")))]
// test the file_write functionality
fn test_write_to_file() -> crate::Result<()> {
@@ -164,5 +164,5 @@ mod test {
assert_eq!(data, contents);
Ok(())
}
} */
}