| 1 | #! /bin/false | ||
| 2 | |||
| 3 | # Copyright (C) 2016-2018 Guido Flohr <guido.flohr@cantanea.com>, | ||
| 4 | # all rights reserved. | ||
| 5 | |||
| 6 | # This program is free software: you can redistribute it and/or modify | ||
| 7 | # it under the terms of the GNU General Public License as published by | ||
| 8 | # the Free Software Foundation; either version 3 of the License, or | ||
| 9 | # (at your option) any later version. | ||
| 10 | |||
| 11 | # This program is distributed in the hope that it will be useful, | ||
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 14 | # GNU General Public License for more details. | ||
| 15 | |||
| 16 | # You should have received a copy of the GNU General Public License | ||
| 17 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 18 | |||
| 19 | package Qgoda::Init::node; | ||
| 20 | $Qgoda::Init::node::VERSION = 'v0.9.2'; | ||
| 21 | use strict; | ||
| 22 | |||
| 23 | use Locale::TextDomain qw('qgoda'); | ||
| 24 | use File::Spec; | ||
| 25 | |||
| 26 | use Qgoda::Util qw(read_file write_file merge_data); | ||
| 27 | |||
| 28 | sub new { | ||
| 29 | my ($class, $init) = @_; | ||
| 30 | |||
| 31 | bless { | ||
| 32 | __init => $init, | ||
| 33 | }, $class; | ||
| 34 | } | ||
| 35 | |||
| 36 | sub run { | ||
| 37 | my ($self, $config) = @_; | ||
| 38 | |||
| 39 | my $q = Qgoda->new; | ||
| 40 | my $logger = $q->logger; | ||
| 41 | my $init = $self->{__init}; | ||
| 42 | |||
| 43 | my $npm = $init->getOption('npm'); | ||
| 44 | |||
| 45 | my @cmd = ($npm, 'init', '--yes'); | ||
| 46 | push @cmd, '--force' if $init->getOption('force'); | ||
| 47 | |||
| 48 | if (!$init->command(@cmd)) { | ||
| 49 | $logger->error(__"Cannot setup asset processing."); | ||
| 50 | return; | ||
| 51 | } | ||
| 52 | |||
| 53 | my @dev_deps = @{$config->{_node_dev_dependencies} || []}; | ||
| 54 | if (@dev_deps) { | ||
| 55 | @cmd = ($npm, 'add', '--save-dev', @dev_deps); | ||
| 56 | $init->command(@cmd); | ||
| 57 | } | ||
| 58 | |||
| 59 | my @deps = keys @{$config->{_node_dependencies} || []}; | ||
| 60 | if (@deps) { | ||
| 61 | @cmd = ($npm, 'install', '--save', @deps); | ||
| 62 | $init->command(@cmd); | ||
| 63 | } | ||
| 64 | |||
| 65 | return $self if !$config->{_package}; | ||
| 66 | |||
| 67 | if ($config->{_package}) { | ||
| 68 | $logger->info(__"updating 'package.json'"); | ||
| 69 | |||
| 70 | my $json = JSON->new; | ||
| 71 | my $json_data = read_file 'package.json' | ||
| 72 | or $logger->fatal(__x("cannot read '{filename}': {error}", | ||
| 73 | filename => 'package.json', | ||
| 74 | error => $!)); | ||
| 75 | |||
| 76 | my $old = eval { $json->decode($json_data) }; | ||
| 77 | $logger->fatal($@) if $@; | ||
| 78 | |||
| 79 | my $data = merge_data $old, $config->{_package}; | ||
| 80 | $json->pretty(1); | ||
| 81 | $json_data = $json->encode($data); | ||
| 82 | |||
| 83 | write_file 'package.json', $json_data | ||
| 84 | or $logger->fatal(__x("cannot write '{filename}': {error}", | ||
| 85 | filename => 'package.json', | ||
| 86 | error => $!)); | ||
| 87 | } | ||
| 88 | |||
| 89 | return $self; | ||
| 90 | } | ||
| 91 | |||
| 92 | 1; |