react-native-integrate
Version:
Automate integration of additional code into React Native projects
49 lines (48 loc) • 2.4 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.waitForFile = waitForFile;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const picocolors_1 = __importDefault(require("picocolors"));
const prompter_1 = require("../prompter");
const getProjectPath_1 = require("./getProjectPath");
async function waitForFile(filePath) {
return new Promise((resolve, reject) => {
// Check if the file already exists
const relativePath = path_1.default.relative((0, getProjectPath_1.getProjectPath)(), filePath);
if (fs_1.default.existsSync(filePath)) {
(0, prompter_1.logMessage)(`file already exists at ${picocolors_1.default.yellow(relativePath)}`);
resolve(false);
return;
}
// Create a watcher to monitor changes in the directory
try {
const onCancel = () => {
(0, prompter_1.stopSpinner)(picocolors_1.default.gray('user requested to skip this step'));
watcher.close();
reject(new Error('skip'));
};
(0, prompter_1.startSpinner)(`waiting you to manually place the file to ${picocolors_1.default.yellow(relativePath)} (press [s] to skip)`, onCancel);
const normalizedPath = path_1.default.normalize(filePath);
const watcher = fs_1.default.watch(
// Extract the directory path from the file path
filePath.split(path_1.default.sep).slice(0, -1).join(path_1.default.sep), { persistent: false }, // Set persistent as false to automatically close the watcher when no longer needed
(event, filename) => {
// Check if the file has been created
if (event === 'rename' &&
filename === normalizedPath.split(path_1.default.sep).pop()) {
// Stop watching
(0, prompter_1.stopSpinner)(`file was placed to ${picocolors_1.default.yellow(relativePath)}`);
watcher.close();
resolve(true);
}
});
}
catch (e) {
reject(e instanceof Error ? e : new Error(e?.toString() ?? ''));
}
});
}