The fixImports function resolves missing imports in a project by finding matching files based on their syntax structure and updating package.json to include new dependencies. It uses the fs, path, and importer modules to find code files, resolve imports, and update package.json.
npm run import -- "fix project paths"var fs = require('fs')
var path = require('path')
var importer = require('../Core')
var {relativeImports} = importer.import("builtin and local modules")
var {glob} = importer.import("glob files")
function fixImports(project) {
var packagePath = path.join(project, 'package.json')
var codeFiles = glob('**/*.+(ts|js|ipynb)', project)
codeFiles.forEach(f => {
var imports = relativeImports(f)
// TODO: find file based on matching syntax structure
imports.missing.forEach(i => {
var match = codeFiles
.filter(f => path.basename(f) === path.basename(i))[0]
if(match) {
var newRelative = './' + path.relative(
path.dirname(f), path.join(project, match))
var fixFile = fs.readFileSync(f).toString()
fs.writeFileSync(f, fixFile.replace(i, newRelative))
}
})
// fix package json
if(fs.existsSync(packagePath)) {
var packageJson = JSON.parse(fs.readFileSync(packagePath))
var toAdd = imports.packages
.filter(p => typeof packageJson.dependencies[p] === 'undefined'
&& typeof packageJson.devDependencies[p] === 'undefined')
toAdd.forEach(a => {
console.log(`adding ${a}`)
packageJson.dependencies[a] = 'latest'
})
if(toAdd.length > 0) {
fs.writeFileSync(packagePath,
JSON.stringify(packageJson, null, 4))
}
}
})
}
module.exports = {
fixImports
}
const fs = require('fs');
const path = require('path');
const { importer } = require('../Core');
const { relativeImports, glob } = importer.import('builtin and local modules');
/**
* Fix imports for a given project by:
* - Resolving missing imports in code files
* - Adding missing packages to package.json
*
* @param {string} project - Path to the project root
*/
function fixImports(project) {
const packagePath = path.join(project, 'package.json');
const codeFiles = glob('**/*.+(ts|js|ipynb)', project);
// Resolve missing imports in each code file
codeFiles.forEach((file) => {
const { missing } = relativeImports(file);
// Find matching file based on syntax structure (not implemented)
// const matchingFile = findMatchingFile(codeFiles, missing);
// if (matchingFile) {
// const newRelative = './' + path.relative(
// path.dirname(file), path.join(project, matchingFile));
// const fixFileContent = fs.readFileSync(file).toString();
// fs.writeFileSync(file, fixFileContent.replace(missing, newRelative));
// }
// Replace missing imports with relative paths
missing.forEach((importPath) => {
const match = codeFiles.find((f) => path.basename(f) === path.basename(importPath));
if (match) {
const newRelative = './' + path.relative(
path.dirname(file), path.join(project, match));
const fixFileContent = fs.readFileSync(file).toString();
fs.writeFileSync(file, fixFileContent.replace(importPath, newRelative));
}
});
// Fix package.json to add missing dependencies
if (fs.existsSync(packagePath)) {
const packageJson = JSON.parse(fs.readFileSync(packagePath));
const toAdd = relativeImports(file).packages
.filter((pkg) => typeof packageJson.dependencies[pkg] === 'undefined'
&& typeof packageJson.devDependencies[pkg] === 'undefined');
toAdd.forEach((pkg) => {
console.log(`Adding ${pkg} as dependency`);
packageJson.dependencies[pkg] = 'latest';
});
if (toAdd.length > 0) {
fs.writeFileSync(packagePath, JSON.stringify(packageJson, null, 4));
}
}
});
}
module.exports = {
fixImports,
};fixImports Function Breakdown
The fixImports function corrects import statements in a project by:
fs module for file system operations.path module for file path manipulation.importer module with relativeImports and glob functions.project: The project directory path.glob to find all code files (TS, JS, and IPYNB) in the project.fixImports function.var fs = require('fs'): Imports the file system module.var path = require('path'): Imports the path module.var importer = require('../Core'): Imports the importer module from the ../Core directory.var {relativeImports} = importer.import('builtin and local modules'): Imports the relativeImports function from the importer module.var {glob} = importer.import('glob files'): Imports the glob function from the importer module.var codeFiles = glob('**/*.+(ts|js|ipynb)', project): Finds all code files in the project using the glob function.var imports = relativeImports(f): Resolves the import statements in a code file using the relativeImports function.var packagePath = path.join(project, 'package.json'): Constructs the path to the package.json file.var match = codeFiles.filter(f => path.basename(f) === path.basename(i))[0]: Finds a matching file in the code files array.fs.readFileSync(f).toString(): Reads a file content as a string.fs.writeFileSync(f, fixFile.replace(i, newRelative)): Writes a file with updated content.packageJson.dependencies[a] = 'latest': Adds a new dependency to the package.json file.fs.writeFileSync(packagePath, JSON.stringify(packageJson, null, 4)): Writes the updated package.json file.