const fs = require('fs'); const schemaSql = fs.readFileSync('schema.sql', 'utf8'); var schemaSqlTrimmed = schemaSql.replace(/^\n$/g, ''); schemaSqlTrimmed = schemaSqlTrimmed.replace(/\(\n/g, '('); schemaSqlTrimmed = schemaSqlTrimmed.replace(/\n /g, ' '); schemaSqlTrimmed = schemaSqlTrimmed.replace(/\n\)/g, ')'); schemaSqlTrimmed = schemaSqlTrimmed.replace(/,\n/g, ''); for (let i =0 ; i<10; i++){ schemaSqlTrimmed = schemaSqlTrimmed.replace(/\n\n/g, '\n'); } // split schemaSqlTrimmed on 'by \n' const schemaSqlSplit = schemaSqlTrimmed.split('\n'); const commentIndex = schemaSqlSplit.findIndex(line => line.match(/^COMMENT/)); const alterIndex = schemaSqlSplit.findIndex(line => line.match(/^ALTER/)); console.log({commentIndex, alterIndex}); const createSql = schemaSqlSplit.slice(0, commentIndex).join('\n'); fs.writeFileSync('create.sql', createSql); const commentSql = schemaSqlSplit.slice(commentIndex, alterIndex).join('\n'); fs.writeFileSync('comment.sql', commentSql); const alterSql = schemaSqlSplit.slice( alterIndex).join('\n'); fs.writeFileSync('alter.sql', '-- \n'+'-- alter.sql\n'+ '-- \n' + alterSql); // console.log({schemaSqlTrimmed})