Compare commits

...

1 Commits

Author SHA1 Message Date
Florian Rival
869257f059 [WIP] Add Flow typing to optionalRequire 2019-04-19 18:11:30 +01:00
9 changed files with 32 additions and 24 deletions

View File

@@ -1,4 +1,4 @@
// Note: this file don't use export/imports nor Flow to allow its usage from Node.js
// Note: this file does not use export/imports and use Flow comments to allow its usage from Node.js
const optionalRequire = require('../../Utils/OptionalRequire.js');
const electron = optionalRequire('electron');

View File

@@ -34,6 +34,7 @@ export default class LocalExamples extends Component<Props, State> {
componentDidMount() {
findExamples(examplesPath => {
if (!fs) return;
fs.readdir(examplesPath, (error, exampleNames) => {
if (error) {
console.error('Unable to read examples:', error);
@@ -57,7 +58,7 @@ export default class LocalExamples extends Component<Props, State> {
createFromExample = (exampleName: string) => {
const { outputPath } = this.state;
if (!fs || !outputPath) return;
if (!fs || !outputPath || !path) return;
findExamples(examplesPath => {
fs.mkdirsSync(outputPath);

View File

@@ -7,18 +7,18 @@ const app = electron ? electron.remote.app : null;
const fs = optionalRequire('fs');
const process = optionalRequire('process');
const tryPath = (
path: string,
onExists: string => void,
onNoAccess: Function
) =>
fs.access(path, fs.constants.R_OK, err => {
if (!err) onExists(path);
else onNoAccess();
});
export const findExamples = (cb: (?string) => void) => {
if (!process || !fs) return '';
if (!process || !fs || !path) return '';
const tryPath = (
path: string,
onExists: string => void,
onNoAccess: Function
) =>
fs.access(path, fs.constants.R_OK, err => {
if (!err) onExists(path);
else onNoAccess();
});
const appPath = app ? app.getAppPath() : process.cwd();

View File

@@ -66,6 +66,8 @@ export default class LocalProjectOpener {
autoSavePath: string,
compareLastModified: boolean
): boolean => {
if (!fs) return false;
if (fs.existsSync(autoSavePath)) {
if (!compareLastModified) {
return true;

View File

@@ -100,7 +100,7 @@ export default class ResourcesEditor extends React.Component<Props, State> {
openProjectFolder = () => {
const project = this.props.project;
if (shell) shell.openItem(path.dirname(project.getProjectFile()));
if (shell && path) shell.openItem(path.dirname(project.getProjectFile()));
};
openProperties = () => {

View File

@@ -22,7 +22,7 @@ export const openJfxr = ({
resourcePath,
extraOptions,
}: ExternalEditorOpenOptions) => {
if (!electron || !ipcRenderer) return;
if (!electron || !ipcRenderer || !path) return;
const projectPath = path.dirname(project.getProjectFile());
const initialResourcePath = getLocalResourceFullPath(
project,

View File

@@ -23,7 +23,7 @@ export const openPiskel = ({
onChangesSaved,
extraOptions,
}: ExternalEditorOpenOptions) => {
if (!electron || !ipcRenderer) return;
if (!electron || !ipcRenderer || !path) return;
const resources = resourceNames.map((resourceName, originalIndex) => {
let resourcePath = getLocalResourceFullPath(project, resourceName);

View File

@@ -82,10 +82,12 @@ export default class ResourcesList extends React.Component<Props, State> {
};
_locateResourceFile = (resource: gdResource) => {
if (!path) return;
const resourceFolderPath = path.dirname(
getLocalResourceFullPath(this.props.project, resource.getName())
);
electron.shell.openItem(resourceFolderPath);
if (electron) electron.shell.openItem(resourceFolderPath);
};
_openResourceFile = (resource: gdResource) => {
@@ -93,7 +95,7 @@ export default class ResourcesList extends React.Component<Props, State> {
this.props.project,
resource.getName()
);
electron.shell.openItem(resourceFilePath);
if (electron) electron.shell.openItem(resourceFilePath);
};
_copyResourceFilePath = (resource: gdResource) => {
@@ -101,13 +103,15 @@ export default class ResourcesList extends React.Component<Props, State> {
this.props.project,
resource.getName()
);
electron.clipboard.writeText(resourceFilePath);
if (electron) electron.clipboard.writeText(resourceFilePath);
};
_scanForNewResources = (
extensions: string,
createResource: () => gdResource
) => {
if (!path || !glob) return;
const project = this.props.project;
const resourcesManager = project.getResourcesManager();
const projectPath = path.dirname(project.getProjectFile());

View File

@@ -1,4 +1,5 @@
// Note: this file don't use export/imports to allow its usage from Node.js
// @flow
// Note: this file does not use export/imports and use Flow comments to allow its usage from Node.js
const nodeRequire = require('node-require-function')(); //TODO
/**
@@ -9,11 +10,11 @@ const nodeRequire = require('node-require-function')(); //TODO
* @param {string} moduleName The name of the module. For example: `fs`.
*/
const optionalRequire = (
moduleName,
config = {
moduleName /*: string */,
config /*: ?{|rethrowException: boolean|} */ = {
rethrowException: false,
}
) => {
) /*: ?Object */ => {
try {
if (global.require) {
// Electron will expose require on global object. Use it, with an
@@ -29,7 +30,7 @@ const optionalRequire = (
// We don't have Electron require nor Node.js require (we must be in a browser)
return null;
} catch (ex) {
if (config.rethrowException) throw ex;
if (config && config.rethrowException) throw ex;
console.error(
'Exception while requiring module (from optionalRequire):',