automatically plugin directory to Flipper config on init
Summary: Freshly init-ed plugins are not picked up by Flipper if they are not on the search path. This diff checks if the current dir is on the search path, and, if not, suggests to add it. Reviewed By: jknoxville Differential Revision: D21619632 fbshipit-source-id: b1dbe2695dbee9ce537999dc83e36f969ba4b747
This commit is contained in:
committed by
Facebook GitHub Bot
parent
49b4022228
commit
d103c218d2
@@ -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<string, string[]>(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}'`,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user