diff --git a/desktop/pkg/src/commands/init.ts b/desktop/pkg/src/commands/init.ts index 45897003a..caad462d4 100644 --- a/desktop/pkg/src/commands/init.ts +++ b/desktop/pkg/src/commands/init.ts @@ -15,6 +15,8 @@ import {spawnSync} from 'child_process'; import recursiveReaddirImport from 'recursive-readdir'; import {promisify} from 'util'; import inquirer from 'inquirer'; +import {homedir} from 'os'; + const recursiveReaddir = promisify(recursiveReaddirImport); const templateDir = path.resolve(__dirname, '..', '..', 'templates', 'plugin'); @@ -38,6 +40,9 @@ export default class Init extends Command { public async run() { const {args} = this.parse(Init); + const pluginDirectory: string = path.resolve(process.cwd(), args.directory); + await verifyFlipperSearchPath(pluginDirectory); + const idQuestion: inquirer.QuestionCollection = [ { type: 'input', @@ -55,7 +60,6 @@ export default class Init extends Command { default: id, }, ]; - const pluginDirectory: string = path.resolve(process.cwd(), args.directory); const title: string = (await inquirer.prompt(titleQuestion)).title; const packageName = getPackageNameFromId(id); const outputDirectory = path.join(pluginDirectory, packageName); @@ -68,7 +72,7 @@ export default class Init extends Command { `⚙️ Initializing Flipper desktop template in ${outputDirectory}`, ); await fs.ensureDir(outputDirectory); - initTemplate(id, title, outputDirectory); + await initTemplate(id, title, outputDirectory); console.log(`⚙️ Installing dependencies`); spawnSync('yarn', ['install'], {cwd: outputDirectory, stdio: [0, 1, 2]}); @@ -76,7 +80,9 @@ export default class Init extends Command { console.log( `✅ Plugin directory initialized. Package name: ${packageName}.`, ); - console.log(` Run 'cd ${packageName} && yarn watch' to get started!`); + console.log( + ` Run 'cd ${packageName} && yarn watch' to get started! You might need to restart Flipper before the new plugin is detected.`, + ); } } @@ -114,3 +120,48 @@ export async function initTemplate( } } } + +async function verifyFlipperSearchPath(pluginDirectory: string) { + const flipperConfigPath = path.join(homedir(), '.flipper', 'config.json'); + if (!fs.existsSync(flipperConfigPath)) { + console.warn( + `It seems Flipper is not installed on your machine; failed to find ${flipperConfigPath}. Head to 'fbflipper.com' to download flipper`, + ); + } else { + const config = JSON.parse(fs.readFileSync(flipperConfigPath, 'utf8')); + const pluginPaths: string[] = config.pluginPaths ?? []; + const isInSearchPath = pluginPaths.some( + (p) => pluginDirectory === path.resolve(p.replace(/^~/, homedir())), + ); + if (!isInSearchPath) { + if ( + ( + await inquirer.prompt([ + { + type: 'confirm', + name: 'addToPath', + message: `You are about to create a plugin in a directory that isn't watched by Flipper. Should we add ${pluginDirectory} to the Flipper search path? (Ctrl^C to abort)`, + default: true, + }, + ]) + ).addToPath + ) { + fs.writeFileSync( + flipperConfigPath, + JSON.stringify( + { + ...config, + pluginPaths: [...pluginPaths, pluginDirectory], + }, + null, + 2, + ), + 'utf8', + ); + console.log( + `⚙️ Added '${pluginDirectory}' to the search paths in '${flipperConfigPath}'`, + ); + } + } + } +}