1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- const { copySync, existsSync, removeSync } = require('fs-extra')
- const { resolve, join, normalize } = require('path')
- const copyTemplates = require('./helpers/copy-templates')
- const logger = require('./helpers/logger')
- const log = logger('app:tauri', 'green')
- const warn = logger('app:tauri (template)', 'red')
- const injectConfFile = (injectPath, { force, logging }) => {
- const path = join(injectPath, 'tauri.conf.js')
- if (existsSync(path) && force !== 'conf' && force !== 'all') {
- warn(`tauri.conf.js found in ${path}
- Run \`tauri init --force conf\` to overwrite.`)
- if (!force) return false
- } else {
- try {
- removeSync(path)
- copySync(resolve(__dirname, './templates/tauri.conf.js'), path)
- } catch (e) {
- if (logging) console.log(e)
- return false
- } finally {
- if (logging) log('Successfully wrote tauri.conf.js')
- }
- }
- }
- const injectTemplate = (injectPath, { force, logging, tauriPath }) => {
- const dir = normalize(join(injectPath, 'src-tauri'))
- if (existsSync(dir) && force !== 'template' && force !== 'all') {
- warn(`Tauri dir (${dir}) not empty.
- Run \`tauri init --force template\` to overwrite.`)
- if (!force) return false
- }
- const tauriDep = tauriPath ? `{ path = "${join('..', tauriPath, 'tauri')}" }` : null
- try {
- removeSync(dir)
- copyTemplates({
- source: resolve(__dirname, './templates/src-tauri'),
- scope: {
- tauriDep
- },
- target: dir
- })
- } catch (e) {
- if (logging) console.log(e)
- return false
- } finally {
- if (logging) log('Successfully wrote src-tauri')
- }
- }
- /**
- *
- * @param {string} injectPath
- * @param {string} type ['conf'|'template'|'all']
- * @param {string|boolean} [force=false] - One of[false|'conf'|'template'|'all']
- * @param {boolean} [logging=false]
- * @param {string} [tauriPath=null]
- * @returns {boolean}
- */
- const inject = (injectPath, type, { force = false, logging = false, tauriPath = null }) => {
- if (typeof type !== 'string' || typeof injectPath !== 'string') {
- warn('- internal error. Required params missing.')
- return false
- }
- if (type === 'conf' || type === 'all') {
- injectConfFile(injectPath, { force, logging })
- }
- if (type === 'template' || type === 'all') {
- injectTemplate(injectPath, { force, logging, tauriPath })
- }
- return true
- }
- module.exports = {
- inject
- }
|