SyntaxError when trying to change files (JSON) - json

This code has suddenly stopped working in Visio Studio Code. it is suppose to change the JSON file text I got this code from https://www.youtube.com/watch?v=OWdyi5EGxq4. here's the code below. Help would be appreciated thanks.
const fs = require("fs");
const { json } = require("stream/consumers");
const args = process.argv.slice(2);
const inputFolder = args[0];
const dir = `${__dirname}/${inputFolder}/`;
const inputFiles = fs.readdirSync(dir).sort();
inputFiles.forEach((file) => {
let id = file.split(".").shift();
let data = JSON.parse(fs.readFileSync(`${dir}/${file}`));
data.name = `ipfwork/${id}`;
fs.writeFileSync(`${dir}/${file}`, JSON.stringify(data, null, 2));
console.log(data);
});

Related

CheerioJS to parse data on script tag

I've been trying to parse the data that is in the script tag using cheerio however It's been difficult for the following reasons.
Can't parse string that is generated into JSON because of html-entities
More Info:
Also what is strange to me is that you have to re-load the content into cheerio a second time to get the text.
Your welcome to fork this replit or copy and paste the code to try it yourself
https://replit.com/#Graciasc/Cheerio-Script-Parse
const cheerio = require('cheerio')
const {decode} = require('html-entities')
const html = `
<body>
<script type="text/javascript"src="/data/common.0e95a19724a68c79df7b.js"></script>
<script>require("dynamic-module-registry").set("from-server-context", JSON.parse("\x7B\x22data\x22\x3A\x7B\x22available\x22\x3Atrue,\x22name\x22\x3A"Gracias"\x7D\x7D"));</script>
</body>
`;
const $ = cheerio.load(html, {
decodeEntities: false,
});
const text = $('body').find('script:not([type="text/javascript"])');
const cheerioText = text.eq(0).html();
//implement a better way to grab the string
const scriptInfo = cheerio.load(text.eq(0).html()).text();
const regex = new RegExp(/^.*?JSON.parse\(((?:(?!\)\);).)*)/);
const testing = regex.exec(scriptInfo)[1];
// real output:
//\x7B\x22data\x22\x3A\x7B\x22available\x22\x3Atrue,\x22name\x22\x3A"Gracias"\x7D\x7D when logged
console.log(testing)
// Not Working
const json = JSON.parse(testing)
const decoding = decode(testing)
// same output as testing
console.log(decoding)
// Not working
console.log('decode', JSON.parse(decoding))
//JSON
{ Data: { available: true, name: 'Gracias' } }
A clean solution is to use JSDOM
repl.it link( https://replit.com/#Graciasc/Cheerio-Script-Parse#index.js)
const { JSDOM } = require('jsdom')
const dom = new JSDOM(`<body>
<script type="text/javascript"src="/data/common.0e95a19724a68c79df7b.js"></script>
<script>require("dynamic-module-registry").set("from-server-context", JSON.parse("\x7B\x22data\x22\x3A\x7B\x22available\x22\x3Atrue,\x22name\x22\x3A"Gracias"\x7D\x7D"));</script>
</body>`)
const serializedDom = dom.serialize()
const regex = new RegExp(/^.*?JSON.parse\("((?:(?!"\)\);).)*)/gm);
const jsonString = regex.exec(serializedDom)[1];
console.log(JSON.parse(jsonString))
// output: { data: { available: true, name: 'Gracias' } }

Add Revit Levels and 2D Minimap to your 3D for local viewer

In our project, we use a local viewer and we do not receive data from a request, but we have it locally.
And getting data from our local files it is impossible to insert it into the panel.
And the call doc.downloadAecModelData() returns null.
See more https://forge.autodesk.com/blog/add-revit-levels-and-2d-minimap-your-3d
Autodesk.Viewing.Initializer(options, async () => {
const config3d = {
useConsolidation: true,
useADP: false,
extensions: [
"Autodesk.Viewing.MarkupsCore",
"MarkupExtension",
"Autodesk.AEC.LevelsExtension",
"Autodesk.AEC.Minimap3DExtension"
]
};
const init_div = document.getElementById("init_div");
const svf_path = "/storage/" + decodeURIComponent(props.search.split("&&")[1]);
const p = "/storage/" + props.search.split("&&")[1];
const viewer = new Autodesk.Viewing.GuiViewer3D(init_div, config3d);
Autodesk.Viewing.endpoint.getItemApi = (endpoint, derivativeUrn, api) => {
return (
"/storage/5bf49c73e2ac26b2756b642ae1b29037/aa1bb3d5baaa229e0c15e674adb7aceec1a0fb061e9c0288d9979f801fb6460d.svf/Resource" +
decodeURIComponent(p.split("Resource")[1])
);
};
const paths = svf_path.split("/");
const [dest, svf_dir] = [paths[2], paths[3]];
const url = http://localhost:3000/api/viewer/dest/${dest}/svf/${svf_dir}/manifest;
const response = await fetch(url);
const manifest = await response.json();
const aec_url = /api/viewer/dest/${dest}/svf/${svf_dir}/aec;
const response_aec = await fetch(aec_url);
const aec = JSON.parse(await response_aec.json());
// console.log(aec);
const viewerDocument = new Autodesk.Viewing.Document(manifest);
const a = await viewerDocument.downloadAecModelData();
console.log(a);
console.log(manifest);
const viewable = viewerDocument.getRoot().getDefaultGeometry();
viewable.data.aec_odel_data = aec;
// console.log(viewable);
await Autodesk.Viewing.Initializer(options, () => {
// const aec = viewable.parent.children[1].data; //.urn//.split("Resource")[1];
// for resize and use viewer outer useEffect
props.set_loc_viewer(viewer);
viewer.start();
viewer.loadDocumentNode(viewerDocument, viewable, options);
});
As i know you can only use doc.downloadAecModelData() after document loaded, like this:
onDocumentLoadSuccess(doc: Autodesk.Viewing.Document) {doc.downloadAecModelData(); }

How to display PDF from a byte64 string on my angular template in Angular6

I am working on PDF view and download for my angular app, here i would want to download pdf on a section, but before that i want to display the pdf on my angular template, which i am stuck at, how could i accomplish this, kindly request assistance, thanks in advance.
This is the code i am using for download, how could i change this so i could even make it viewable on my UI.
onSubmitPolicyPDF() {
let formValuePolicyPDF: any = {
cst_policy_number: this.submitprop.data.policy_number,
}
this.policyService.policyPDF(formValuePolicyPDF)
.pipe()
.subscribe(poldownloaddata => {
this.poldownloaddata = poldownloaddata,
console.log('poldownloaddata: ', poldownloaddata);
console.log(atob(this.poldownloaddata.data.pdf_ccnt));
const linkSource = 'data:application/pdf;base64,' + this.poldownloaddata.data.pdf_ccnt;
// const linkSource = (atob(this.previewdata.data.pdf_ccnt));
const downloadLink = document.createElement("a");
const fileName = "policy.pdf";
downloadLink.href = linkSource;
downloadLink.download = fileName;
downloadLink.click();
(error: any) => this.errorMessage = <any>error
}
)
}

Imported generated JSON in JSX causes Webpack build loop

I've got a small postcss plugin I've made that generates a JSON file off a colors.css variable file during webpack build.
My postcss plugin
const fs = require('fs');
const postcss = require('postcss');
const capitalize = (string) => string.charAt(0).toUpperCase() + string.slice(1);
const getPropName = (string) => {
let name = clean(string.split('-'));
name.shift();
for(let k = 1; k < name.length; k++){ //start at 1 to skip 'color' prefix
name[k] = capitalize(name[k].toString());
}
return name.join('');
};
const clean = (array) => {
let i = array.length;
while(i--){
if (!array[i]) {
array.splice(i, 1);
i++;
}
}
return array;
};
module.exports = postcss.plugin('cssobject', (files, filters, options) =>
(css) => {
options = options || {
destination: ''
};
// Processing code will be added here
const getVariable = (variable) => {
let result;
css.walkRules((rules) => {
rules.walkDecls((decl) => {
const pointer = variable.replace('var(', '').replace(')','');
if(!decl.prop.match(pointer)) return;
result = decl.value;
});
});
return result;
};
css.walkRules((rules) => { //hooks into CSS stream
let i = files.length;
let cssObject = {};
while (i--) {
if(!rules.source.input.from.match(files[i])) return; //scrubs against requested files
rules.walkDecls((decl) => {
let j = filters.length;
while(j--){
if(!decl.prop.match(filters[j])) return; //scrubs against requested rules
let prop = getPropName(decl.prop);
cssObject[prop] = (decl.value.match('var'))? getVariable(decl.value) : decl.value;
}
});
}
if (options.destination) {
fs.writeFile(options.destination, JSON.stringify(cssObject), 'utf8');
}
});
}
);
I'm then importing this JSON file into a react component JSX file to then parse JSON data into a visual guide of project's used colors under AA and AAA requirements... anywho
The problem I'm having is my webpack-dev-server keeps re-building over and over again cause it thinks a change has been made to the JSX file, when in fact it's only ever a change to the JSON file being imported.
Is there a standard way of importing generated files in to a JSX without causing infinite build loops?
I've already tried having the JSON file be saved well outside of the webpack dev's watch location, and still build loop remains.
Thanks in advance!
you can change you file's timestamp, the webpack will not build after you change your file
const now = Date.now() / 1000;
const lastModifyTime = now - 11;
const lastAccessTime = now - 11;
fs.utimesSync(jsonPath, lastModifyTime, lastAccessTime);
Have a try, hope to help you.

Convert .txt file to JSON

I want to convert a fairly unorganized and unstructured text file to JSON format. I want to be able to use the city ID information. Is there anyway I can convert this to JSON?
UPDATE: I also found this solution after a while. Very simple way to get the JSON of any tab separated text file.
https://shancarter.github.io/mr-data-converter/
You can try to use tsv2json this tool can reads a tsv file from stdin and writes a json file to stdout.
It's distributed in source file, to compile it you need to download D compiler and then run dmd tsv2json.d.
If you have more complex task there is another tool named tsv-utils
TSV to JSON in nodejs
var file_name = 'city_list.txt';
var readline = require('readline');
var fs = require('fs');
var lineReader = readline.createInterface({
input: fs.createReadStream(file_name)
});
var isHeader = false;
var columnNames = [];
function parseLine(line) {
return line.trim().split('\t')
}
function createRowObject(values) {
var rowObject = {};
columnNames.forEach((value,index) => {
rowObject[value] = values[index];
});
return rowObject;
}
var json = {};
json[file_name] = [];
lineReader.on('line', function (line) {
if(!isHeader) {
columnNames = parseLine(line);
isHeader = true;
} else {
json[file_name].push(createRowObject(parseLine(line)));
}
});
lineReader.on('close', function () {
fs.writeFileSync(file_name + '.json', JSON.stringify(json,null,2));
});