Flow: [libs] in .flowconfig to use globs?

Created on 6 Jul 2017  路  4Comments  路  Source: facebook/flow

Would be nice to be able to use globs or regex patterns.

[libs]
; doesn't work :(
node_modules/@company/**/flow/
node_modules/@company/.*/flow/

; works but tedious :S
node_modules/@company/module-one/flow/ 
node_modules/@company/module-two/flow/
; ...
node_modules/@company/module-four-hundred/flow/

See flowtype/flow-bin#39.

feature request

Most helpful comment

So to fix this temporarily I created this simple script and I adjusted my package.json to run everytime before flow.

@tur-nr in your case: just update the const FLOW_LIB_DIRS_TITLE fro __flow__ to flow

The script is very simple and straight forward, it just looks tedious because I explained everything in the comments.

.flowconfig:

[ignore]
./lib
./build
./node_modules
.*/node_modules/babel-plugin-flow-runtime

[include]

[libs]
# @start generated __flow__ dir paths
./src/__flow__
# @end generated __flow__ dir paths

[options]

[lints]

initFlow.js :

/* eslint import/no-extraneous-dependencies: 0 */
const path = require('path');
const fs = require('fs');
const glob = require('glob');

/** the title of the directories to include under `[lib]` in `.flowconfig`. */
const FLOW_LIB_DIRS_TITLE = '__flow__';
/** `.flowconfig` file path */
const FLOWCONFIG_FILE_PATH = path.join(__dirname, '../.flowconfig');

/** path injection text marks placed as comments in `.flowconfig` */
const INJECTION_START_MARK_TEXT = `# @start generated ${FLOW_LIB_DIRS_TITLE} dir paths`;
const INJECTION_END_MARK_TEXT = `# @end generated ${FLOW_LIB_DIRS_TITLE} dir paths`;

/** adds the paths to all the dirs available in the project with the title FLOW_LIB_DIRS_TITLE
  * to `.flowconfig` under the [libs] section.
  */
const initFlow = () => {
  /** read the file and split the text */
  const flowConfigFileSplitText = fs
    .readFileSync(FLOWCONFIG_FILE_PATH, 'utf8')
    .split('\n');

  /** get the indexes of the INJECTION_START_MARK_TEXT and INJECTION_END_MARK_TEXT */
  const indexOfStartMark = flowConfigFileSplitText.indexOf(
    INJECTION_START_MARK_TEXT
  );
  const indexOfEndMark = flowConfigFileSplitText.indexOf(
    INJECTION_END_MARK_TEXT
  );

  /** throw an error if the path injection text marks were not found in the `.flowconfig` */
  if (indexOfStartMark === -1 || indexOfEndMark === -1) {
    throw new Error(
      'Flow initialization script failed, could not fin paths injection markers.'
    );
  }

  /** get the split text of the file before the marker (including the marker) */
  const flowConfigFileSplitTextFirstPart = flowConfigFileSplitText.slice(
    0,
    indexOfStartMark + 1
  );
  /** get the split text of the file after the marker (including the marker) */
  const flowConfigFileSplitTextSecondPart = flowConfigFileSplitText.slice(
    indexOfEndMark,
    flowConfigFileSplitText.length
  );

  /** get all the directories with the title FLOW_LIB_DIRS_TITLE */
  glob(`**/${FLOW_LIB_DIRS_TITLE}`, null, (err, files) => {
    /** 
     * concat the split text arrays into a single one.
     * prefix the paths with `./` to follow the flowconfig syntax.
     * concat the single split text array items using a new line character.
     */
    const newFlowConfigFileText = flowConfigFileSplitTextFirstPart
      .concat(files.map(filePath => `./${filePath}`))
      .concat(flowConfigFileSplitTextSecondPart)
      .reduce((textSoFar, item) => `${textSoFar}\n${item}`, '')
      .slice(1);
    /** write the flow file back to disk */
    fs.writeFileSync(FLOWCONFIG_FILE_PATH, newFlowConfigFileText, 'utf8');
  });
};

/** call the initFlow function */
initFlow();

All 4 comments

Any updates on this?
How can we use a regular expression to target the libs folder?

I'd prefer it if I can have _flow_ folders in my components and then use

[libs]
.*/_flow_/*

As of now, there is no way to do this.

So to fix this temporarily I created this simple script and I adjusted my package.json to run everytime before flow.

@tur-nr in your case: just update the const FLOW_LIB_DIRS_TITLE fro __flow__ to flow

The script is very simple and straight forward, it just looks tedious because I explained everything in the comments.

.flowconfig:

[ignore]
./lib
./build
./node_modules
.*/node_modules/babel-plugin-flow-runtime

[include]

[libs]
# @start generated __flow__ dir paths
./src/__flow__
# @end generated __flow__ dir paths

[options]

[lints]

initFlow.js :

/* eslint import/no-extraneous-dependencies: 0 */
const path = require('path');
const fs = require('fs');
const glob = require('glob');

/** the title of the directories to include under `[lib]` in `.flowconfig`. */
const FLOW_LIB_DIRS_TITLE = '__flow__';
/** `.flowconfig` file path */
const FLOWCONFIG_FILE_PATH = path.join(__dirname, '../.flowconfig');

/** path injection text marks placed as comments in `.flowconfig` */
const INJECTION_START_MARK_TEXT = `# @start generated ${FLOW_LIB_DIRS_TITLE} dir paths`;
const INJECTION_END_MARK_TEXT = `# @end generated ${FLOW_LIB_DIRS_TITLE} dir paths`;

/** adds the paths to all the dirs available in the project with the title FLOW_LIB_DIRS_TITLE
  * to `.flowconfig` under the [libs] section.
  */
const initFlow = () => {
  /** read the file and split the text */
  const flowConfigFileSplitText = fs
    .readFileSync(FLOWCONFIG_FILE_PATH, 'utf8')
    .split('\n');

  /** get the indexes of the INJECTION_START_MARK_TEXT and INJECTION_END_MARK_TEXT */
  const indexOfStartMark = flowConfigFileSplitText.indexOf(
    INJECTION_START_MARK_TEXT
  );
  const indexOfEndMark = flowConfigFileSplitText.indexOf(
    INJECTION_END_MARK_TEXT
  );

  /** throw an error if the path injection text marks were not found in the `.flowconfig` */
  if (indexOfStartMark === -1 || indexOfEndMark === -1) {
    throw new Error(
      'Flow initialization script failed, could not fin paths injection markers.'
    );
  }

  /** get the split text of the file before the marker (including the marker) */
  const flowConfigFileSplitTextFirstPart = flowConfigFileSplitText.slice(
    0,
    indexOfStartMark + 1
  );
  /** get the split text of the file after the marker (including the marker) */
  const flowConfigFileSplitTextSecondPart = flowConfigFileSplitText.slice(
    indexOfEndMark,
    flowConfigFileSplitText.length
  );

  /** get all the directories with the title FLOW_LIB_DIRS_TITLE */
  glob(`**/${FLOW_LIB_DIRS_TITLE}`, null, (err, files) => {
    /** 
     * concat the split text arrays into a single one.
     * prefix the paths with `./` to follow the flowconfig syntax.
     * concat the single split text array items using a new line character.
     */
    const newFlowConfigFileText = flowConfigFileSplitTextFirstPart
      .concat(files.map(filePath => `./${filePath}`))
      .concat(flowConfigFileSplitTextSecondPart)
      .reduce((textSoFar, item) => `${textSoFar}\n${item}`, '')
      .slice(1);
    /** write the flow file back to disk */
    fs.writeFileSync(FLOWCONFIG_FILE_PATH, newFlowConfigFileText, 'utf8');
  });
};

/** call the initFlow function */
initFlow();

Thanks @A-Tokyo for your script.

However, I find this solution a bit hacky and I'm still looking for a more integrated solution.

Any idea on this ?

Coming across this. So lame this isnt a feature.

Was this page helpful?
0 / 5 - 0 ratings