Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reset imported src files on each call to flatten #4

Merged
merged 1 commit into from
Dec 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion helpers/find-file.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const fs = require('fs')
const glob = require('glob-promise')
const path = require('path')
const { importedSrcFiles } = require('./variables')

const { getImportedSrcFiles } = require('./variables')
const constants = require('./constants')
const changeRelativePathToAbsolute = require('./change-relative-path-to-absolute')

Expand Down Expand Up @@ -56,6 +57,9 @@ async function byNameAndReplaceInnerRecursively(importStatement, updatedFileCont
}

async function byNameAndReplaceInnerRecursivelyInner(importStatement, updatedFileContent, dir, dependencyPath, srcFiles, j, resolve, reject, importIsReplacedBefore) {
// Get the variable storing imported src files.
const importedSrcFiles = getImportedSrcFiles()

if (j >= srcFiles.length) return resolve({ flattenFileContent: updatedFileContent, importIsReplacedBefore })

let isAbsolutePath = !dependencyPath.startsWith(constants.DOT)
Expand Down
6 changes: 5 additions & 1 deletion helpers/replace-all-imports-in-current-layer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const fs = require('fs')
const path = require('path')
const { importedSrcFiles } = require('./variables')

const { getImportedSrcFiles } = require('./variables')
const constants = require('./constants')
const findFile = require('./find-file')
const updateImportObjectLocationInTarget = require('./update-import-object-location-in-target')
Expand All @@ -15,6 +16,9 @@ async function replaceAllImportsInCurrentLayer(i, importObjs, updatedFileContent
}

async function replaceAllImportsInCurrentLayerInner(i, importObjs, updatedFileContent, dir, resolve) {
// Get the variable storing imported src files.
const importedSrcFiles = getImportedSrcFiles()

if (i >= importObjs.length) {
return resolve(updatedFileContent)
}
Expand Down
10 changes: 9 additions & 1 deletion helpers/variables.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,12 @@ function processVariables(args) {
return variables
}

module.exports = { processVariables, importedSrcFiles }
function resetImportedSrcFiles () {
importedSrcFiles = {}
}

function getImportedSrcFiles () {
return importedSrcFiles
}

module.exports = { processVariables, resetImportedSrcFiles, getImportedSrcFiles }
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const path = require('path')
const fs = require('fs')

const { processVariables } = require('./helpers/variables')
const { processVariables, resetImportedSrcFiles } = require('./helpers/variables')
const log = require('./helpers/logger')
const constants = require('./helpers/constants')
const replaceAllImportsRecursively = require('./helpers/replace-all-imports-recursively')
Expand All @@ -29,6 +29,9 @@ async function main(args) {
}

async function flatten(inputFilePath) {
// Reset the global variable to store imported source files.
resetImportedSrcFiles()

inputFilePath = path.resolve(inputFilePath)

const inputFileContent = fs.readFileSync(inputFilePath, 'utf8')
Expand Down