Home > Net >  fs.writeSync insert text in a var
fs.writeSync insert text in a var

Time:11-02

hello i'm trying to insert my text in my var Directory in my config file is it possible ? i'm trying to use position but the problem with position is if my text is long it does not fit well

 const defaultFolderName = fs.readFileSync('config.js', {encoding: 'utf8'});
    
    const folderName = process.argv[2] || defaultFolderName;
    const fd = fs.openSync('config.js', 'r ');
    
    const PathName = folderName;
    
    const data = `${PathName}', `;
    
    fs.writeSync(fd, data, 246, 'utf8');

config.js

const config = {

  Directory: 'insertText',  

  movie: 'test',  


};

export default config;

CodePudding user response:

This seems very misguided, but you can do it with a regular expression replacement.

if (process.argv[2]) {
    const oldConfig = fs.readFileSync('config.js', {encoding: 'utf8'});
    newConfig = oldConfig.replace(/Directory:\s*'.*',/, `Directory: '${process.argv}',`);
    fs.writeFileSync('config.js', newConfig, {encoding: 'utf8'});
}
  • Related