-1

I have following codes in a file. Please note that VS code inserts a new line after export let theadClass = when I save the file.

<script lang="ts">
    export let header: Array<string>;
    export let divClass: string = 'relative overflow-x-auto shadow-md sm:rounded-lg';
    export let tableClass: string = 'w-full text-sm text-left text-gray-500 dark:text-gray-400';
    export let theadClass: string =
        'text-xs text-gray-700 uppercase bg-gray-50 dark:bg-gray-700 dark:text-gray-400';
</script>

<div class={divClass}>
// more after this line ...
</div>

Ideally I'd like to extract as following to create a prop table. But it is ok if I extract strings between export to ; as well.

header: Array<string>;
divClass:string = 'relative overflow-x-auto shadow-md sm:rounded-lg';
tableClass:string = 'w-full text-sm text-left text-gray-500 dark:text-gray-400';
theadClass:string = 'text-xs text-gray-700 uppercase bg-gray-50 dark:bg-gray-700 dark:text-gray-400';

When I use the following funciton:

import * as fs from 'fs';

export function getLines(fileName, keyword) { 
  let outputs =[];
  const file = fs.readFileSync(fileName, {encoding: 'utf-8'});
  let arr = file.split(/\r?\n/);
  arr.forEach((line, idx) => {
    if(line.includes(keyword)){
      outputs.push(line);
    }
  });
  return outputs
}

I get the following:

 [
  '\texport let header: Array<string>;',
  "\texport let divClass: string = 'relative overflow-x-auto shadow-md sm:rounded-lg';",
  "\texport let tableClass: string = 'w-full text-sm text-left text-gray-500 dark:text-gray-400';",
  '\texport let theadClass: string ='
]

As you can see it, the last line is not complete.

How can I extract lines that have export to ;?

shin
  • 31,901
  • 69
  • 184
  • 271
  • Please consider changing the regex here: `let arr = file.split(/\r?\n/);`. Currently, it is splitting using new-line/carriage-return - which is why the last line `theadClass` is left hanging at the `'='` symbol. – jsN00b Apr 07 '22 at 00:44

1 Answers1

0

I'm guessing the problem is the newline that VSCode inserts newline automatically. Would it be viable for you to have a .prettierrc with a larger printWidth? If not, add //prettier-ignore before the last line?

Or alternatively, split the file using ; instead of \n in let arr = file.split(/\r?\n/);.

cSharp
  • 2,884
  • 1
  • 6
  • 23
  • Thanks. It seems working. But I have a lot of files like this. Do I have to go through file by file to fix this? Or is there any way I can do it by command? – shin Apr 07 '22 at 00:51
  • @shin which way did you try? If you go the adding-`//prettier-ignore` route, you will have to go file by file. If you have a rc file, then you'll probably have to open each file once and save them in order to format them with the new rc file. Alternatively, there is [are ways](https://stackoverflow.com/questions/43666270/how-do-i-format-all-files-in-a-visual-studio-code-project) to help you format all files in a project. – cSharp Apr 07 '22 at 01:00